sed string with any order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed string with any order
# 1  
Old 08-30-2013
RedHat sed string with any order

Hi, i have a strange prob. log file contains ip, protocol, user name, agent . these can be in any order. If log contains the above order able to fetch all details but if details are in diff order not able to fetch all details.

using below command.
Code:
grep -A50 "Entry " "/logs/file.log" \
|grep -A50 "Login" \
|sed -n -e '/IPADDR/p' -e '/PROTOCOL/p' -e '/USER_NM/p' -e '/AGENT/p' \
| sed 's/^ *//g' \
|sed -e 's/IPADDR */1C /g' -e 's/PROTOCOL */2C /g' -e 's/USER_NM */4C /g' -e 's/AGENT */3C /g'  > $TMP_FILE

Can someone plz help

thanks in advance

Last edited by Scott; 08-30-2013 at 08:32 PM.. Reason: Code tags, please...
# 2  
Old 08-30-2013
Come on Satyak... With more than 100 posts in these forums you know that we can't help if you only show us code that doesn't work. Show us your input, show us the corresponding desired output, and explain the logic behind the transformation you want to occur.
# 3  
Old 08-31-2013
RedHat

Hi Dan,here are the details.

Input:
Code:
Entry
   CLIENT                httpd
   ATION_ENABLED         1
   ACCOUNT_NAME
   HTTPUSERAGENT         Jakarta Commons-HttpClient/3.0
   USER_NM            	 satya
   AGENT       			Jakarta Commons-HttpClient/3.0
   CLIENTADDR            148.171.38.52
   COOKIE                Cookie-20130827-085956-satya-27,183,264
   TARGETDIR
   HTTP_COOKIE           $Version=0; fx=544d63766232756d5454436c33716256697036414f413d3d; $Path=/
   USERGID               100
   ACCOUNT_ID
   USERCOOKIE            Cookie-20130827-085956-satya-27,183,264
                         /bin/mutt
   HTTP_CONTENT_TYPE     application/x-www-form-urlencoded
   HOMEDIR               /users/00000214011
   HTTP_CONTENT_LENGTH   1397
   IMEOUT                900
   IPADDR	              1.2.3.4
   XFERTYPE              I
   USERTYPE              virtual
   TYPE                  httpcmd
   REMOTEHOST            1.2.3.4
   USERCLASS             VirtClass
   PROTOCOL              http
   APPLICATION_TYPE
   LOGFILENAME          /var/logs/agent_error.log
   TRIGGER               post
   CLIENTPID             25555
   DXAGENT_LOGINPASS             **********
   DXAGENT_SECURE_DATA           1
   DXAGENT_TARGET
   DXAGENT_SESSIONID             69192fb3c09f374d3d41f7c5471f7ef4
   LIBPATH                       /lib/
   DXAGENT_EXTRAARGS             http

Expected output:
Code:
IP : 1.2.3.4
Protocol: http
User Name: satya
Agent: Jakarta Commons-HttpClient/3.0

Thanks
satya

Last edited by Scott; 08-31-2013 at 03:04 AM.. Reason: Code tags]
# 4  
Old 08-31-2013
Whit more than 100 post why do you not use code tags?

Using awk
Code:
awk '/IPADDR/ {print "IP:", $2} /PROTOCOL/ {print "Protocol:",$2 "\nUser Name:",u "\nAgent:",a} /USER_NM/ {u=$2} /^AGENT/{a=$2 " " $3 " " $4}' file
IP: 1.2.3.4
Protocol: http
User Name: satya
Agent: Jakarta Commons-HttpClient/3.0

EDIT: Some more readable version, handles space in username and Agent
Code:
awk '
	/^IPADDR/ {i=$2} 
	/^PROTOCOL/ {p=$2} 
	/^USER_NM/ {$1="";u=$0} 
	/^AGENT/{$1="";a=$0}
	END {print "IP:",i "\nProtocol:",p "\nUser Name:" u "\nAgent:" a}
	' file


Last edited by Jotne; 08-31-2013 at 03:27 AM..
# 5  
Old 08-31-2013
I don't understand your problem - you're not too far off. Slightly modifying your code snippet to
Code:
sed -n -e '/IPADDR/p' -e '/PROTOCOL/p' -e '/USER_NM/p' -e '/ AGENT/p' file

you'll get
Code:
   USER_NM                 satya
   AGENT                   Jakarta Commons-HttpClient/3.0
   IPADDR                  1.2.3.4
   PROTOCOL              http

You could also OR your regexes together (ERE version):
Code:
sed -rn '/IPADDR|PROTOCOL|USER_NM| AGENT/p' file

# 6  
Old 08-31-2013
awk can do a precise (string) check on $1 (first word)
Code:
awk '
$1=="IPADDR" {i=$2} 
$1=="PROTOCOL" {p=$2} 
$1=="USER_NM" {$1=""; u=$0} 
$1=="AGENT" {$1=""; a=$0}
END {
  print "IP:",i
  print "Protocol:",p
  print "User Name:" u
  print "Agent:" a
}
' file

# 7  
Old 09-01-2013
Quote:
Originally Posted by MadeInGermany
awk can do a precise (string) check on $1 (first word)
Code:
awk '
$1=="IPADDR" {i=$2} 
$1=="PROTOCOL" {p=$2} 
$1=="USER_NM" {$1=""; u=$0} 
$1=="AGENT" {$1=""; a=$0}
END {
  print "IP:",i
  print "Protocol:",p
  print "User Name:" u
  print "Agent:" a
}
' file

Thank you. I can see my self using this quite a bit.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - searching token in certain order

Hello. I would like to write a bash function which would return "true" if the search succeed else return anything else. something like if ] ; then exit 1 fi function my_funct () { find first occurrence $2 in $1 if not found return "false" from that position,... (6 Replies)
Discussion started by: jcdole
6 Replies

2. Shell Programming and Scripting

Manipulate XML File Continous STRING by each Order Line using SHELL

heres sample File: <?xml version="1.0"?> <!DOCTYPE cXML SYSTEM "www"><cXML.............................................. <OrderRequest>USE UNIX.com</Extrinsic><Extrinsic name="UniqueName">Peter@UNIX.com</Extrinsic><Extrinsic name="ContractingEntity">UNIX... (3 Replies)
Discussion started by: Pete.kriya
3 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

sed or awk to order a file

Hi - I have a file with lots of lines in that I need to order based on the number of commas! e.g the file looks something like :- cn=john,cn=users,cn=uk,dc=dot,dc=com cn=john,cn=users,dc=com cn=users,cn=groups,dc=com cn=john,cn=admins,cn=users,cn=uk,dc=dot,dc=com... (4 Replies)
Discussion started by: sniper57
4 Replies

5. UNIX for Dummies Questions & Answers

How to change the order of a string ?

Hi , I want to change the order of a string using sed command . Is it possible ? $echo "abc123xyz" | sed 's/\()*\) \(*\)/\2\1/' abc123xyz $ echo "abc123xyz" |sed 's/\()*\) \(*\) \()*\)/\2\1\3/' abc123xyz I want to change the string , abc123xyz as xyz123abc . Is it... (5 Replies)
Discussion started by: rajavu
5 Replies

6. Shell Programming and Scripting

Reversing file order using SED

Im trying to develop a shell script that will change the content order of the file. For example I have a file that says a b c d I want to change this to be d c b a Im trying to use sed to this by reading the file and then inserting each line at the top #!/usr/bin/ksh ... (3 Replies)
Discussion started by: MBGPS
3 Replies

7. Shell Programming and Scripting

HELP !! Builting string in incremental order

I have files in a folder like .. citi11082006_1.trn citi11082006_2.trn citi11082006_3.trn ... ... ... citi11082006_13.trn .,... citi11082006_nn.trn Each file will have one string inside and I am builting a master string based out the string in the sub files by merging all into one by... (6 Replies)
Discussion started by: u263066
6 Replies

8. UNIX for Dummies Questions & Answers

Changing the order using sed

I have a text "abc def ghi" and I want to get it as "def abc ghi" I am using this echo "abc def ghi" | sed 's/\(*\)\(*\)/\2\1/' But I am not able to get the output, could anyone help me. Thanks (9 Replies)
Discussion started by: venu_nbk
9 Replies

9. Shell Programming and Scripting

output string in reversing order

If I have string { I_love_shell_scripts} anyone knows how to have output {stpircs_llehs_evol_I} by using shell and perl ?I know in perl, there is reverse() funcation, but can it be done by not using reverse()? (3 Replies)
Discussion started by: ccp
3 Replies

10. UNIX for Dummies Questions & Answers

using sed and regex to reverse order???

so i have been trying to learn how to manipulate text on my own and have gotten stumped... let's say i have a text file that says (highly simplified): people ordinary How would swap the order of the words.. I know i need to use sed and some kind of back reference but cannot make it... (2 Replies)
Discussion started by: urtherhoda
2 Replies
Login or Register to Ask a Question