Using sed to find text between a "string " and character ","


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to find text between a "string " and character ","
# 1  
Old 12-13-2011
Using sed to find text between a "string " and character ","

Hello everyone
Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried

Code:
sed -n 's/.*string \([^ ]*\),.*/\1/p' file

with some, but limited success. This gives out all the occurances of only one particular string, when in the file the first 2 strings will always be different. Using this command, sed has given every 3rd instance I think.

I would then like to append these obtained strings to a file, each on a new line, prefixed by another "string "

Thanks in advance to anyone who can assist.

Last edited by haggismn; 12-13-2011 at 07:18 PM..
# 2  
Old 12-13-2011
It would be easier if you post some sample data and expected output.
# 3  
Old 12-13-2011
I am scanning for IP addresses from a log file. The logfile being scanned contains the sets of IPs and the correct delimiters on the same line, but that line is never in a set place.

Code:
line x
line y
aaabbb ccc-ddd string ipaddress,eee string ipaddress2,fff string ipaddress3,ggg
line z

This line is then repeated further on. Its not needed really but is scanning the lines in reverse order possible?

The results then need to be output in the form of "string2 ipaddress", taking a new line each time with 2 or 3 in total. A script will be necessary there won't it?

Currently I am getting all the instances of ipaddress3 in the log file
Thanks again

Last edited by haggismn; 12-13-2011 at 07:18 PM..
# 4  
Old 12-13-2011
Do you want only on sed?

If you use awk and you have always the same format, could you use:
Code:
soy@machine: temporal > cat uno.txt
line x
line y
aaabbb ccc-ddd string ipaddress,eee string ipaddress2,fff string ipaddress3,ggg
line z
soy@machine: temporal > awk -F"," '/string/ {  print $1 " " $2 " " $3 }' uno.txt | awk '{ print $3, $4, $6, $7, $9, $10 }'
string ipaddress string ipaddress2 string ipaddress3

# 5  
Old 12-13-2011
Try this, it also deals with the newline:

Code:
awk '{gsub(/,/,"\n");}; {for(i=2;i<=NF-1;i+=3) {print ($(i+1),($(i+2)))}}' infile

This User Gave Thanks to verdepollo For This Post:
# 6  
Old 12-13-2011
Thanks Maya_style and verdepollo.

Verdepollo's awk command nearly works. I have altered it slightly. I was not specific enough in that there are also commas throughout the file where they are not needed. This works, but I don't think it is optimal. The log file may sometimes be quite large and it is to be run on an embedded device. Can cpu time be reduced by finding away around the grep command? Thanks again


Code:
awk '{gsub(/,/,"\n");gsub(/string1/,"string2");}; {for(i=1;i<=NF-1;i+=3) {print ($(i),($(i+1)))}}' log |grep -m 3 string2

---------- Post updated at 09:51 PM ---------- Previous update was at 07:49 PM ----------

It seems as though I am having problems. I need to implement this as soft coded configuration, echo'ed into a script. When I do this I lose the quotation marks around "\n" and "string2". This was the main reason I initially preferred a sed command, as it may possibly avoid problems like this. Can anyone give advice on getting round this problem with quotations? To give an example
Code:
awk '{gsub(/,/,"\n");gsub(/string1/,"string2");}; {for(i=1;i<=NF-1;i+=3) {print ($(i),($(i+1)))}}' log |grep -m 3 string2

becomes
Code:
awk '{gsub(/,/,\n);gsub(/string1/,string2);}; {for(i=1;i<=NF-1;i+=3) {print ( $ (i),( $ (i+1)))}}' log |grep -m 3 string2

and is therefore not working any more.

Can I get around this or do I need to look more towards sed?

Thanks a million
# 7  
Old 12-13-2011
Try this...
Code:
echo 'awk \'{...}\' | grep -m 3 string2 ' >> yourfile

If you can post a actual data sample I would like to have a look once again.

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question