0

This is the simple awk command i am trying to write

grep "Inputs - " access.log | awk 'BEGIN { FS = "Inputs -" } ; { print $2 }'

i am trying to grep the file access.log for all the lines with "Input -" and trying to awk the part after the "Input -". This is giving the following error

awk: syntax error near line 1 
awk: bailing out near line 1

I am confused what is the issue with this, this should work!!!!

I have also tried the following and it does not work

grep "Inputs - " L1Access.log | awk -F='Inputs' '{print $1}'

Here is a sample input text file

This is line number 1. I dont want this line to be part of grep output
This is line number 2. I want this line to be part of grep output. This has "Input -", I want to display only the part after "Input -" from this line using awk
user1241438
  • 1,533
  • 4
  • 17
  • 24

4 Answers4

1

Since you stated you want everything after the first instance "Inputs -", and since your grep is unnecessary:

nawk -F"Inputs -" 'BEGIN {OFS="Inputs -"} {line=""}; { for(i=2;i<=NF;i++) line=line OFS $i} {print line}' test

Your own answer will only print out the second element. In the event that you have more than one "Input -" you will be missing the remaining of the line. If you don't want the second (or third.. ) "Inputs -" in the output you could use:

nawk -F"Input -" '{ for(i=2;i<=NF;i++) print $i}' test
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Both will not work for me. The reason being what if the lines had multiple ' - '. Also i want to be able to have a field seperator which is more than 1 character – user1241438 Oct 24 '14 at 13:59
  • You stated that you wanted every thing after the "Inputs - ". Is there more than one "Inputs - " in the record? You also say you want to have a field seperator which is more than one character. The field seperator I've used here is three " - ", but I don't see what you would "want" that. Please add an example of your more complex record so we can properly help you. – JNevill Oct 24 '14 at 14:09
1

your problem cannot be reproduced here:

kent$  cat f
foo - xxx
foo - yyy
foo - zzz
fooba
kent$  grep 'foo - ' f| awk 'BEGIN { FS = "foo -"};{print $2}' 
 xxx
 yyy
 zzz

There must be something wrong in your awk codes. Besides, if you want to do a grep and awk to extract the part after your Inputs - you can use grep to do it in single shot:

kent$  grep -Po 'foo - \K.*' f
xxx
yyy
zzz
Kent
  • 189,393
  • 32
  • 233
  • 301
0

OK folks i see what my issue is. I am using solaris and in solaris the awk does not have capability for regex, meaning it does not support more than 1 charater in the field seperator. So i used nawk

Please refer to this post Stackoverflow post

grep "Inputs - " L1Access.log | nawk 'BEGIN { FS = "Inputs -" }  { print $2 }'

this worked.

Community
  • 1
  • 1
user1241438
  • 1,533
  • 4
  • 17
  • 24
0

You are not clear on what to get. Here is a sample file:

cat file
test Inputs - more data
Here is nothing to get
yes Inputs - This is what we need Inputs - but what about this?

You can then use awk to get data:

awk -F"Inputs - " 'NF>1 {print $2}' file
more data
This is what we need

or like this?

awk -F"Inputs - " 'NF>1 {print $NF}' file
more data
but what about this?

By setting separator to Inputs - and test for NF>1 it will only print lines with Inputs -

Jotne
  • 40,548
  • 12
  • 51
  • 55
  • like i said in my other comments, awk -F works for only single charater field seperator in solaris. What you have said may work fine in other operating system, but not in solaris. You have to use nawk in solaris – user1241438 Oct 26 '14 at 17:45
  • @user1241438 are there any reason for not using `nawk` in solaris? – Jotne Oct 26 '14 at 20:59