sed / awk to get specific word in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed / awk to get specific word in line
# 1  
Old 03-21-2012
sed / awk to get specific word in line

I have http log that I want to get words after specific "tag", this a sample line from the log:

Code:
98,POST,200 OK,www.facebook.com,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,/ajax/updatestatus.php?__a=1,datr=P_H1TgjTczCHxiGwdIF5tvpC; lu=Si1fMkcrU2SInpY8tk_7tAnw; c_user=728445064; xs=61%3Ab9ee26a8f2fc53efb960a6bd6c1c0042%3A0%3A1328685197; presence=EDvFA22A2EtimeF1328685268EuserFA2728445064A2EstateFDutF1328685268207EvisF1EvctF0H0EblcF0EsndF1ODiFA21014445485A2C_5dEfFA21014445485A2EuctF1328685227EsF0CEchFDp_5f728445064F15CC; p=3; act=1328685304452%2F17%3A2; _e_0Hb1_9=%5B%220Hb1%22%2C1328685304455%2C%22act%22%2C1328685304452%2C17%2C%22http%3A%2F%2Fwww.facebook.com%2Fajax%2Fupdatestatus.php%22%2C%22f%22%2C%22submit%22%2C%22wall%22%2C%22r%22%2C%22%2Fmeemelati%22%2C%7B%22ft%22%3A%7B%7D%2C%22gt%22%3A%7B%22profile_owner%22%3A%22731612557%22%2C%22ref%22%3A%22mf%22%7D%7D%2C0%2C0%2C0%2C0%2C16%5D; x-src=%2Fajax%2Fupdatestatus.php%7Cprofile_stream_composer,548,application/x-www-form-urlencoded; charset=UTF-8,0,application/x-javascript; charset=utf-8,gzip,chunked,post_form_id=a012a7a073bc1d990a6c449643ea4570&fb_dtsg=AQDnwj7O&xhpc_composerid=ux0ih_13&xhpc_targetid=731612557&xhpc_context=profile&xhpc_fbx=1&xhpc_timeline=&xhpc_ismeta=1&xhpc_message_text=Hello%20Londoners&xhpc_message=Hello%20Londoners&composertags_place=102173726491792&composertags_place_name=&composer_predicted_city=102173726491792&composer_session_id=1328685296&is_explicit_place=&composertags_city=102173726491792&disable_location_sharing=false&nctr[_mod]=pagelet_wall&lsd&post_form_id_source=AsyncRequest&__user=728445064&phstamp=16581681101191065579516,<EOH>

After awk found specific tag: "xhpc_message_text="
It will give output: "Hello Londoners" (it will remove url character encoding too, like "%20" in this sample output string)
And limit by char "&" or "&xhpc_message".

thanks, for any suggestion to solve this problem.
# 2  
Old 03-21-2012
with awk you can use function 'index' to get position of the tag.
'substr' can be used to cut from this position to the end.
another 'index' call will find out next position of '&'.
now you have string position of your expected result.
# 3  
Old 03-21-2012
ok, I'll try that
# 4  
Old 03-21-2012
awk

Hi,

Try this one,

Code:
awk -F"&xhpc_message_text=" '{l=substr($2,0,match($2,"&")-1);gsub(/%20/," ",l);print l;}' file

Cheers,
RangaSmilie
# 5  
Old 03-21-2012
you can retrieve the value using awk. but converting the URL-encoding to ascii is the task here..

Code:
 
$ nawk -F\& '{for(i=1;i<=NF;i++)if($i~/xhpc_message_text/){split($i,a,"=");print a[2]}}' test.txt
Hello%20Londoners

In perl, you can do it easily. let me know if you are interested in perl
# 6  
Old 03-21-2012
itkamaraj, thank you, I like your solution...
# 7  
Old 03-21-2012
Code:
$ perl -F\& -lane 'foreach(@F){if($_=~m/xhpc_message_text/){($a,$b)=split("=",$_);$b=~tr/+/ /;$b=~s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;$b=~s/<!--(.|\n)*-->//g;print $b}}' input.txt
Hello Londoners

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

In Vi "sed" substitute word on a specific line

i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ... (5 Replies)
Discussion started by: pimmit22043
5 Replies

3. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

4. Shell Programming and Scripting

sed or awk, cut, to extract specific data from line

Hi guys, I have been trying to do this, but... no luck so maybe you can help me. I have a line like this: Total Handled, Received, on queue Input Mgs: 140 / 14 => 0 I need to, get the number after the / until the =, to get only 14 . Any help is greatly appreciated. Thanks, (4 Replies)
Discussion started by: ocramas
4 Replies

5. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

6. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

7. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. UNIX for Dummies Questions & Answers

Using SED to change a specific word's color?

Ok so all i'm trying to do here is output a file and change the color of a specific word. I can't use grep with color because I need all lines of the file not just lines that match the pattern. I can get this substitution to work but when it displays it shows exactly what i'm putting it rather... (14 Replies)
Discussion started by: MrEddy
14 Replies

10. Shell Programming and Scripting

Can "sed" substitute word on a specific line?

Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish (2 Replies)
Discussion started by: minifish
2 Replies
Login or Register to Ask a Question