Shell for displaying specific records from a line.

Input file.

GMDCOM.27936 [Tue Oct  1 13:32:40 2013]: Process Request <36812974>
GMDCOM.27936 [Tue Oct  1 13:37:38 2013]: Process Request <36812985>
GMDCOM.27936 [Tue Oct  1 13:37:53 2013]: Process Request <36812986>
GMDCOM.27936 [Tue Oct  1 13:37:54 2013]: Process Request <36812987>
GMDCOM.27936 [Tue Oct  1 13:37:57 2013]: Process Request <36812996>
GMDCOM.27936 [Tue Oct  1 13:38:06 2013]: Process Request <36812998>
GMDCOM.27936 [Tue Oct  1 13:38:14 2013]: Process Request <36813001>
GMDCOM.27936 [Tue Oct  1 13:38:20 2013]: Process Request <36813004>
GMDCOM.27936 [Tue Oct  1 13:38:27 2013]: Process Request <36813007>
GMDCOM.27936 [Tue Oct  1 13:38:29 2013]: Process Request <36813009>

Output file should be in the below format.

36812974 [Tue Oct  1 13:32:40 2013]
36812985 [Tue Oct  1 13:37:38 2013]
36812986 [Tue Oct  1 13:37:53 2013]

and it continues till the end of the file and displays all the records in the above format.

I suggest you read the man page of the cut command. Field separator is per default a single blank, but you can specify any other character with the -d '<character>' option.

I hope this helps.

bakunin

Not giving the desired output.

Or try something like this:

awk -F'[]<>[]' '{print $4, "[", $2, "]"}' file

Please help me by explaining the code.

Hi,

Please use the following code.

sed 's/GMDCOM.27936 //g;s/\://g;s/\<//g;s/\>//g' file_name | awk '{print$8" "$1" "$2" "$3" "$4" "$5}'

Output will be as follows.

36812974 [Tue Oct 1 133240 2013]
36812985 [Tue Oct 1 133738 2013]
36812986 [Tue Oct 1 133753 2013]
36812987 [Tue Oct 1 133754 2013]
36812996 [Tue Oct 1 133757 2013]
36812998 [Tue Oct 1 133806 2013]
36813001 [Tue Oct 1 133814 2013]
36813004 [Tue Oct 1 133820 2013]
36813007 [Tue Oct 1 133827 2013]
36813009 [Tue Oct 1 133829 2013]

 

Thanks,
R. Singh

It uses any of the character "[" "]" "<" or ">" as field separators (specified by the -F command line option). It then prints field 4 and field 2, the second embedded in "[" and " ]".

You cat also try this:

cat input_file | awk '{print $NF,$2,$3,$4,$5,$6}' | tr -d "<>" | sed 's/.\{1\}$//'

kunal,

There is no need for cat as awk takes it.

so it may be as follows.

awk '{print $NF,$2,$3,$4,$5,$6}' input_file | tr -d "<>" | sed 's/.\{1\}$//'

Thanks,
R. Singh

Thanks for correcting me, Yes we can do that with awk only

Kunal

@RavinderSingh13:

sed 's/GMDCOM.27936 //g

Why only for GMDCOM.27936? What happens when there are records with something else in field 1? The . should be escaped: \. . The global flag (g) is not necessary

s/\://g;s/\<//g;s/\>//g

These characters do not need a "\" . You can put this in one statement like so:

s/[:<>]//g
awk '{print$8" "$1" "$2" "$3" "$4" "$5}' 

Since the default OFS is a single space, you can use , instead:

awk '{print $8, $1, $2, $3, $4, $5}'

---
@kunal37

sed 's/.\{1\}$//' 

is the same as

sed 's/.$//'
1 Like

one more solution with sed for given sample :

sed -e 's/.*\(\[.*\]\).*<\(.*\)>$/\2 \1/' input_file

---
@kunal37

sed 's/.\{1\}$//' 

is the same as

sed 's/.$//'

[/quote]

Thanks Scrutinizer :slight_smile:

Hi Ravinder,

I have understood the awk portion of the code. But please can you explain the SED portion.

@gosh_tanmoy: Could you provide a bit of feedback? What solutions did / did not work. If not all of them worked, what is your OS and version?