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 replace a comma to a semicolon in a textfile? 2

Status
Not open for further replies.

zanzemaj

Programmer
Nov 2, 2006
35
NZ
Hi, I'm a UNIX scripting beginner and need some help. I now have a textfile wherein the contents come from SQL query. The number of lines will be dynamic. Here is a sample contents of the textfile named temp1.txt:
(int.sars),
(int.prev),
(int.batchg),
PROCESS DATA TABLES
(int.keelj),
(int),

I need to transform temp1.txt so that the last line before the line PROCESS DATA TABLES will contain a semicolon instead of comma. The desired output is:
(int.sars),
(int.prev),
(int.batchg);
PROCESS DATA TABLES
(int.keelj),
(int),

Any help is greatly appreciated. Thank you very much.
 
Easy approach is to go directly to that line and change it!

vi temp1.txt

For more information about the vi look into this link:


(you will just have to go to that line using arrow keys and then go to the comma then use the combination of ESC+r then just hit the semi-colon key to replace the comma!)

Regards,
Khalid
 
Thank you Khalid. But I need to change this inside a UNIX script. I'm not sure what to use, will I use grep, sed, awk or any other function available.

Here's a part of my script:

Code:
echo "NEW FILE" > new_file.txt
### get output of SQLS
FSQLSet 
cat temp1.txt >> new_file.txt

FSQLSet is my function that writes the contents from SQL query.

Here is a sample output file (temp1.txt) produced by FSQLSet
Code:
(int.sars),
(int.prev),
(int.batchg),
PROCESS DATA TABLES
(int.keelj),
(int),

Any help is greatly appreciated. Thank you very much.
zanzemaj
 
I tried the solution given by feherke and it gives me the desired output.
Thank you feherke for this valuable post.
 
I have a follow-up question. How do I use cut/paste to replace spaces with delimiter? I tried to cut the first field but the output is wrong. I am not sure how to proceed.

Sample record from a file named temp2.txt:
Code:
50fkknm0998 test_table 2007-05-21 12:26:32 9
I tried this:
Code:
cat temp2.txt | cut -f 1 temp2.txt
Result is:
Code:
50fkknm0998 test_table 2007-05-21 12:26:32 9

Or please suggest another way to replace the spaces with delimiter.
 
Which delimiter ? Say a semicolon ?
One way:
Code:
sed 's! !;!g' temp2.txt > temp3.txt

Another (faster) way:
Code:
tr ' ' ';' < temp2.txt > temp3.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you PHV for the quick response. I tried both and it worked. However, I want to cut the first field, second field up to the last field and save them in a variable.

I tried
Code:
sed 's! !;!g' temp2.txt > temp3.txt
Result is
Code:
50fkknm0998;;test_table;;;;;;;;;;;;;2007-05-21;12:26:32;;;;;;9
when I do a cut
Code:
cut -f 1 temp3.txt > temp4.txt
Result is
Code:
50fkknm0998;;test_table;;;;;;;;;;;;;2007-05-21;12:26:32;;;;;;9
How will I get the first field, second field and up to the last field? I want to get 50fkknm0998 and save it to a variable. I want to do this to the second field and up to the last field.

Really appreciate your help. Thanks.
zanzemaj
 
I want to cut the first field, second field up to the last field and save them in a variable
Code:
eval `awk -F';' '{for(i=1;i<=NF;++i)print "var"i"="$i}' temp3.txt`
echo "var1='$var1' var2='$var2' var3='$var3' ..."


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you for all the replies. I tried it and everything works as expected. [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top