Register
It is currently Mon Sep 01, 2014 10:16 pm

replace column characters + associate with file name


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Nov 19, 2010 10:58 am   

Joined: Tue Oct 05, 2010 11:52 am
Posts: 2
Dear Bashmasters,
I have files with the format name_X-Y.txt. Note that in the name, X can be any letter and can be like one or more times, the same with Y, but they are never the same. For example, a name can be NAME1_AAA-HH.txt.
This file includes a column inside that has consecutively
X
X
X
X
.
.
.
Y
Y
Y
I would like to change the X to be always A and Y to be always B, both in the file names and the column inside. This column is the 14th in the files. However, I do not know how to do it.

Can you please help me with that?

Thank you!


Top
 Profile  
 PostPosted: Mon Nov 22, 2010 8:37 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
I do not understand what you mean by A and B. However, this could help you:
Code:
#! /bin/bash

# FIX: you probably need different values for A and B
a=A
b=B

for file ; do

    # parse the two values
    x=${file%.txt}
    x=${x##*_}
    y=$x
    x=${x%-*}
    y=${y#*-}

    # replace the values
    cut -f14 $file \
        | sed "s/$x/$a/;s/$y/$b/" \
        | paste <(cut -f1-13 $file) - <(cut -f15- $file) \
        > ${file%_*}_$a-$b.txt
done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP