Shell script to parse a line and insert a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to parse a line and insert a word
# 1  
Old 08-17-2009
Question Shell script to parse a line and insert a word

Hi All,
I have a file like this,
data1,data2,[data3],[data4],data5,data6.

i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this .
data1,data2,[/example/string],[data4],data5,data6.

Could you guys help me to get a sed command or any other command to perform my above task.
I tried used a sed command like "echo "s/\[/\example\/string" /g > temp
sed -f temp2 data_file> tst_one.
cat tst_one
But this gives me the output as below,
data1,data2,[/example/string],[/example/string],data5,data6.



Thanks in Advance!,
Giri.
# 2  
Old 08-17-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 08-17-2009
Hi zaxxon,
My apologies for not following the forum standards, since i am very new to this forum i was not aware of this stuffs. I shall keep all the points mentioned by u when posting my new query.

Thanks,
Giri.
# 4  
Old 08-17-2009
What you did is replacing "[" (and not "data3") with "/example/string"
Regards.
# 5  
Old 08-17-2009
No i want to replace data3 and not '['.

Thanks,
Girish
# 6  
Old 08-17-2009
Try this,

sed -e "s/\[data3\]/[\/example\/string\]/g"
# 7  
Old 08-17-2009
Quote:
Originally Posted by girish.raos
No i want to replace data3 and not '['.
We understood that - but this is what you actually did with your regexp:

Quote:
I tried used a sed command like "echo "s/\[/\example\/string" /g > temp
If you want to replace "data3" with "something_else" the regexp for this is:

Code:
s/data3/something_else/

It might be that "data3" is occurring several times in the text and you want to change only the occurrences where "data3" is enclosed in rectangular brackets (change "[data3]", but not, say, "(data3)"), preserving the brackets.

for this the regexp would have to look like:

Code:
s/[data3]/[something_else]/

Notice that i put the the brackets to be preserved in the replacement string. Regexps work like that: first the search-part is searched for and - if it is found at all - everything matching this first part is removed from the line. Then the replacement string is prepared and inserted into the place where the search-string was.

As we matched to brackets before they would have been removed if they are not being reinserted with the replacement.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

2. Shell Programming and Scripting

Korn Shell script to insert at specific line

Hi, I am trying to put together a Korn Shell script to insert at a specific line. The system we use is SunOS 5.10 I can get the line number by using:- num=`sed -n '/export ENV/=' ./tmp.file` Not getting much headway using the above variable's value to insert - export SYBASE=/opt/sybase15... (5 Replies)
Discussion started by: aj8200
5 Replies

3. UNIX for Dummies Questions & Answers

shell script : log to txt and insert new line everytime

Hi, I have this script, while do ps ax|grep 5060 > log.txt echo " " sleep 1 done } I want to actually put a new line everytime the loop is executed in log.txt , but I do not know how to "embed" the echo " " inside the log.txt. (so to say... (1 Reply)
Discussion started by: peuceul
1 Replies

4. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

5. UNIX for Advanced & Expert Users

Shell script to parse apache httpd line

Hello, I have serveral SUN Web servers that need to write a KSH to figure out where Apache is installed and, tar up the directory (for backup purposes) once a month. The trick is that Apache is not installed in same location. So when I do "ps -ef| grep httpd" it shows on some boxes from... (2 Replies)
Discussion started by: afadaghi
2 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Shell Programming and Scripting

Parse file from 2nd line in shell script

Hi, I need to parse input file from 2nd line. Input file contents are, ABC123;20100913115432;2000000;NO; 04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;; 04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5; 04;CCC;09;DDD;601020304;CCCC;1;;;;; For each line, 1] I need to check... (17 Replies)
Discussion started by: Poonamol
17 Replies

8. Shell Programming and Scripting

to insert some word somewhere in the line with shell (or perl)

hello mighty all there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end.. my own knowledge base can say it is... (12 Replies)
Discussion started by: tip78
12 Replies

9. Shell Programming and Scripting

Parse a line which has different word length

Hi All, Please let me know a command to parse the below line and find the words, I have a line like this 40609 39930 In this above line the two words are separted by space.The length of this two words may differ. I want to put 40609 in var_one and 39930 in var_two. Eg. Input line is ... (1 Reply)
Discussion started by: girish.raos
1 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question