Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to copy file say MO-CJ--p MO-CJ--c in a directory and then modify 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi

I have few thousands files in the pattern MO-CJ--p with the 2 middle different characters for eg.,MO-PT--p and the file has the content like this

D D*
G gc
M evt
e M

Now, I need to the copy these files, say, from MO-CJ-P to MO-CJ-c and then replace the 2nd line string from G gp to G gc.

Can any one please help me on how to to this task?

kind regards
Prasad

 
Code:
ls | nawk '/MO.*p/{to=substr($0,0,length($0)-1)"c"; system("sed '\''s/^G gp/G gc/'\'' "$0" > "to);}'

nawk is for Solaris systems ... you might need to change it to 'gawk' for gnu awk, or if your awk does the system command just use that.

if you don't have an awk with the system command, change 'system' to 'print' then cut and paste the output into the terminal and run it.

if you want a less specific answer there are other ways.
 
We like to split jobs like this into the 2 tasks that they truly are.

Step 1: Rename the files:

/bin/ls -1 MO-*-p |
sed -e 's/\(.*\)p$/mv \1p \1c;/' |
ksh

You can test the command builder first without piping
to ksh.

Step 2: Edit the new files:

/bin/ls -1 *c |
head |
while read file ; do

sed -e 's/^G gp/G gc/' $file > $file.fmt
# /bin/mv $file.fmt $file

done

Once we're happy that the $file.fmt is editted
correctly, we can un-comment the mv command
and the comment the head command and let her rip.

/bin/ls -1 *c |
while read file ; do

sed -e 's/^G gp/G gc/' $file > $file.fmt
/bin/mv $file.fmt $file

done

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top