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 ","
# 8  
Old 12-14-2011
Thanks Ahamed. Can you be a little more precise with adding extra parts to the echo/awk command? The current commands are very similar as they are. I am just being introduced to awk and sed so please forgive my lack of knowledge.

Here is some sample data;
Code:
Wed Dec 14 04:41:36 2011 SENT CONTROL [server]: 'PUSH_REQUEST' (status=1)
Wed Dec 14 04:41:36 2011 PUSH: Received control message: 'PUSH_REPLY,route-gateway 197.73.129.164,redirect-gateway def1,dhcp-option DNS 208.67.220.220,dhcp-option DNS 8.8.8.8,dhcp-option DNS 208.67.222.222,route-gateway 197.73.129.164,ping 10,ping-restart 30,ifconfig 197.73.129.170 255.255.255.240'
Wed Dec 14 04:41:36 2011 OPTIONS IMPORT: timers and/or timeouts modified
Wed Dec 14 04:41:36 2011 OPTIONS IMPORT: --ifconfig/up options modified
Wed Dec 14 04:41:36 2011 OPTIONS IMPORT: route options modified
Wed Dec 14 04:41:36 2011 OPTIONS IMPORT: route-related options modified
Wed Dec 14 04:41:36 2011 OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified

It is the DNS server IP's that need to be obtained.

The following creates the resolv.conf
Code:
awk '{gsub(/,/, "\n" );gsub(/DNS/, "nameserver" );}; {for(i=1;i<=NF-1;i+=3) {print ($ (i),($ (i+1)))}}' log | grep -m 3 nameserver > /tmp/etc/resolv.conf

Again your help is much appreciated.
# 9  
Old 12-14-2011
My version...
Code:
awk -F, '/DNS/{for(i=1;i<=NF;i++){if($i~"DNS"){gsub(".* ","nameserver ",$i);print $i}}}' log

I just did an echo and it worked.
Code:
root@bt:/tmp# echo awk -F, '/DNS/{for(i=1;i<=NF;i++){if($i~"DNS"){gsub(".* ","nameserver ",$i);print $i}}}' log
awk -F, /DNS/{for(i=1;i<=NF;i++){if($i~"DNS"){gsub(".* ","nameserver ",$i);print $i}}} log

which is your OS?

--ahamed
# 10  
Old 12-14-2011
It is part of a generic DD-WRT/openwrt script I am creating to allow users to more easily connect to an openvpn server.

The script is stored in the nvram of a DD-WRT router, and when extracted it is executed line by line. There is only one script area available in the http input area, but multiple are files needed, so I need to use echo to create those files, each in the form.

The script is executed everytime the openvpn client successfully connects or reconnects
Code:
echo"#!/bin/sh
if [ -e log ]
awk.......log | grep ... > file
fi
(other commands)
" > script.sh
chmod +x script.sh

The above didnt work, this helped.
Code:
echo"#!/bin/sh
if [ -e log ]"> script.sh
echo awk.......log '| grep ... > file' >>script.sh
echo "fi
(other commands)
" >> script.sh
chmod +x script.sh

However then I realised; the command after echo is still not working. Note that the ' marks have been taken away. to get around this "\'" must be used.
Code:
echo awk -F, \''/DNS/{for(i=NF;i>1;i--){if($i~"DNS"){gsub(".* ","nameserver ",$i);print $i}}}'\' l
og

Note that I changed the for loop to search backwards from the end.

Finally, although not as great an issue; Can awk stop after it gets a specific number of results? There are many lines like this containing DNS values, only the 3 are needed. The file may also get rather long so cutting on cpu time and stopping, once the information is obtained, is preferable.

Thanks again, I think almost there!
# 11  
Old 12-14-2011
Quote:
Originally Posted by haggismn
Finally, although not as great an issue; Can awk stop after it gets a specific number of results? There are many lines like this containing DNS values, only the 3 are needed. The file may also get rather long so cutting on cpu time and stopping, once the information is obtained, is preferable.
Just add a count which you can check and call exit.
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