Find and replace using sed

Hi All,

I have a file as shown below: myFile.dat
#----------------------------------------------------------
dataFile

{
    Name        shiva;
    location     Delhi;
    travelID     IDNumber;
}

4
(
560065
700007
100001
200002
)

#---------------------------------------------------------
i wanted to make couple of changes using sed command.

  1. find and change location to new place
sed -i 's/location.*/location bangalore;/' myFile.dat # this is done 
  1. copy travelID and paste it in front of 4
    result should be like this
    IDNumber    4
    (
    560065 .....etc
  1. put ; after last closing bracket )
    result should be like this
    200002
    );

can some body help me?

Regards,
linuxUser_

So whats the final output? Do you want "travelID IDNumber;" to be there or remove it?
Also are there multiple patterns/blocks like this? Are they sequential?

Hi clx
thanks for the reply.
final output file should be as shown below
#----------------------------------------------------------
dataFile

{
Name shiva;
location Bangalore;
travelID IDNumber;
}

IDNumber 4
(
560065
700007
100001
200002
);

#---------------------------------------------------------

---------- Post updated at 04:22 PM ---------- Previous update was at 04:08 PM ----------

sorry again ... iwant bit more generic way to do it as i will have varying list.
in the above case they are only 4 pin code entries so file will look like

4
(
111111
222222
333333
4444444
)

if i have 5 pincodes then one more pincode ll be added in the list as

5
(
111111
222222
333333
4444444
555555
)

so copying IDNumber and pasting before 4 or 5 should be in more generic way

Regards,
linuxUser_

Try this

awk -v newLocation=Bangalore '
	/^location/ {$2=newLocation";"} 
	/^travelID/ {tID=$2;gsub(";$"," ",tID)} 
	/^\}$/ {f=1} 
	f==1 && /^[0-9]+$/ {$0=tID $0;f=0} 
	{print}' file

Output:

#----------------------------------------------------------
dataFile
{
Name shiva;
location Bangalore;
travelID IDNumber;
}

IDNumber 4
(
560065
700007
100001
200002
)

#----------------------------------------------------------

Note : Its strictly based on the sample you provided.

Thanks a lot clx,

it works fine but printing on terminal, how can i save it? in fact i want to overwrite it.
sorry for bothering you but at the end i need ; after closing bracket like this

444444
);

Regards
linuxUser_

awk -v newLocation=Bangalore '
	/^location/ {$2=newLocation";"} 
	/^travelID/ {tID=$2;gsub(";$"," ",tID)} 
	/^\}$/ {f=1} 
	f==1 && /^[0-9]+$/ {$0=tID $0;f=0} 
	/^\)$/ {$0=$0";"} 
	{print}' file > new_file

Thank you very much.
I have a question, In mean time i tried with different files.
some of my files have IDNumb only 6 letters
n files i created have IDNumber its 8 letters ...
also some ppl write these output files with TrvlID again 6 letters instead of travelID....
I just dont want to bother them ...infact you as well ..but need some help...

can u explain the code how it is working?

Sorry for bothering you

Regards,
linuxUser_

---------- Post updated at 05:57 PM ---------- Previous update was at 05:42 PM ----------

its working perfectly for all cases ...sorry
Thanks a lot :slight_smile:

It works as you see. If you have basic understanding of awk , It works in
/pattern/ {action} mode.

awk -v newLocation=Bangalore '                     #define awk variable newLocation
	/^location/ {$2=newLocation";"}            #look for location line and replace $2 with newLocation plus ";" at the end
	/^travelID/ {tID=$2;gsub(";$"," ",tID)}    #look for travelID line and store the $2 with tID variable, gsub is to remove the ";" from the end
	/^\}$/ {f=1}                               #look for line contains "}" and set a flag f to true
	f==1 && /^[0-9]+$/ {$0=tID $0;f=0}         #look for line immd followed by the upper criteria AND contains only numbers, prefix the file with variable tID
	/^\)$/ {$0=$0";"}                          #suffix ";" after ")"
	{print}' file > new_file #print all the lines.
1 Like

you are so kind :slight_smile:

---------- Post updated 05-08-14 at 12:08 AM ---------- Previous update was 05-07-14 at 06:44 PM ----------

Hi again,
Let me guess you have great touch with C++ as well.
Sorry to bother you. I need little help in shell operations from C++ program.
Here I furnish the details of problem:

  1. Lets say my current working path is myWorkingPath.
  2. In my working path I have list of name directories and each name directory has two more sub directories say A/B.
    (now path to B will be pathB = myWorkingPath/name/A/B)
  3. I have a executable (say run) in myWorkingPath directory, that i wanted to execute from B directory.
  4. From C++ program I can do shell operations like mkdir, rm etc using system("mkdir NewDir"), system("rm file") etc., (these shell operations will done form myWorkingPath)
    but unable to do some shell operations from pathB. How can I communicate pathB for system() operations?

lets say I have saved this path in

pathB="/home/linuxUser/myWorkingPath/name/A/B"

. here name is a variable.

for(i=0;i<nameList.size(); i++){
    pathB="/home/linuxUser/myWorkingPath"/nameList.name(i)/"A/B";
    system("cd pathB"); // to goto that B directory
    system("../../../run"); // to run the executable
}

I know this program is wrong but, may give you clear idea what i wanted to do.

can you help me?

Regards,
linuxUser_