1846597 Members
2737 Online
110256 Solutions
New Discussion

Re: Help with awk script

 
SOLVED
Go to solution
Lisa  Mauer
Regular Advisor

Help with awk script

Hi, well I did an oops. We use RCS to maintain version control on all our cobol programs and copy books, scripts, etc. Well we also use symbolic links to link a program in a customer directory to another customer directory. Well... we decided to move the RCS from 1 server to another, and rcp of course created a copy of the program instead of created the symbolic link. An oops I never thought of until now when I have over 600 links to recreate, including removing the now copied code. Thought I would post a help me as my awk scripting skills are not the best... The good news is , I did create a script that lists all the links, so just in case something would ahppen I do have a backup - basically a butt saver right now :)
What I am trying to do is use this file to 1st - delete the 1st file in the line, so we can remove the copy (which should be a link), 2nd recreate the symbolic link listed in this file (as the second file listed on the line is the actual code, the link needs to be recreated from that point to the 1st file listed on the line). Since I know there are some scripting God's out there, as I work on it, any input would be quite appreciated!!! I was going to attempt to use awk, using the '->' as a field separator. Then put the 2nd filename 1st for the ln -s command to the second filename to relink.
Here is the format of the linklog file that lists 551 lines of all links within our customer directories, I am also attaching the entire file:
lrwxrwxrwx 1 382 15 50 Jan 18 2002 /opt/cs3000/pca/funded
/1004131/script/cnutr105.sh,v -> /opt/cs3000/pca/funded/W43392/script/cnutr105.sh,v

ugh... If I get a solution and you are going to LA, I'll definitely buy you a beer!!! :)
Thanks!
Lisa
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: Help with awk script

Hi Lisa:

See if this fits your needs:

awk '{print "rm",$5;print "ln -s",$7,$5}' filein > fileout

Regards!

...JRF...
John Poff
Honored Contributor
Solution

Re: Help with awk script

Hi Lisa,

I have one way to do it. Using your file of links (named links.txt and removed blank lines at the beginning and the end), I ran this awk command to create a file that will remove the first file and create the symlinks:

awk '{printf("rm %s\nln -s %s %s\n", $9, $11, $9)}' links.txt >links.sh

This creates the links.sh file, which looks like this (first couple of lines):

head -4 links.sh
rm /opt/cs3000/pca/funded/1004131/cobol/PNBUT11F.CBL,v
ln -s /opt/cs3000/pca/funded/W43392/cobol/PNBUT11F.CBL,v /opt/cs3000/pca/funded/
1004131/cobol/PNBUT11F.CBL,v
rm /opt/cs3000/pca/funded/1004131/script/cnutr105.sh,v
ln -s /opt/cs3000/pca/funded/W43392/script/cnutr105.sh,v /opt/cs3000/pca/funded/
1004131/script/cnutr105.sh,v


Does that look like what you are looking for?

JP
Andreas Voss
Honored Contributor

Re: Help with awk script

Hi,

here my quick and dirty awk:

awk '{
f1=$(NF-2)
f2=$NF
print "rm " f1
print "ln -s " f2 " " f1
}' ls_file >link_recreate.sh

then run sh link_recreate.sh

Regards
RAC_1
Honored Contributor

Re: Help with awk script

Not getting what you want to say.

But here is what I did.

(put your list in a file say an)

Seperate the link with command
awk -F " " '{print $9}' an
(this gives listing of links)

Seperate actual files(lat feild in a file an)

awk -F " " '{print $NF}' an

Now I have two lists first is link and second actual list.

Give details what exactly you want.
There is no substitute to HARDWORK
James R. Ferguson
Acclaimed Contributor

Re: Help with awk script

Hi Lisa:

Oops, sorry, I can't count. That should be:

# awk '{print "rm",$5;print "ln -s",$9,$11}' filein > fileout

Regards!

...JRF..
James R. Ferguson
Acclaimed Contributor

Re: Help with awk script

Good Lord, I mean:

awk '{print "rm",$5;print "ln -s",$11,$9}' filein > fileout

...JRF...
John Poff
Honored Contributor

Re: Help with awk script

Lisa,

I just thought of another way to tackle the problem. You could do another backup using find to just grab the symlinks and cpio to backup the symlinks, and then just overwrite the files on the new system.

find /opt/cs3000 -type l | cpio -ocm >/var/tmp/rcslinks.cpio

Copy the rcslinks.cpio to the new system and:

cpio -ivmcu

Or something like that. That should overwrite the files with the symlinks.

JP
James R. Ferguson
Acclaimed Contributor

Re: Help with awk script

Hi (again):

I don't think I can count using the numbers I should :-((

# awk '{print "rm",$9;print "ln -s",$11,$9}'

...JRF...
Lisa  Mauer
Regular Advisor

Re: Help with awk script

Thank you Thank you Thank you!!!! I knew I would get GREAT results from you guys - as always! I ended up using John's script - Thank you John - worked like charm and saved me a lot of time trying to figure it out! I would have tried to use the cpio, but there was a week in between (before we noticed the links were gone) where links were being added and or removed so I didn't want to mess anything new up.
Thanks again!!!
James R. Ferguson
Acclaimed Contributor

Re: Help with awk script

Hi Lisa:

/NO_MORE_POINTS_PLEASE/

I never meant for my bungled typing to receive points -- only the last of my corrections!

The use of the last field (NF) in Andreas Voss' solution is the superior answer. By using relative field numbers, like:

# awk '{print "rm",$(NF-2)9;print "ln -s",$NF,$(NF-2)}' filein > fileout

...you can handle input created by 'ls -l' or 'ls -ls' where the addition of the '-s' option adds another field and shifts the field containing the filename downstream.

/NO_MORE_POINTS_PLEASE/

Regards!

...JRF...
Lisa  Mauer
Regular Advisor

Re: Help with awk script

Geez, sorry about that James. As you can tell I was in a hurry - but also wanted to be sure I assigned points to everyone who took the time to help me out :)
Thanks again!