add special characters in a text using awk - awk

I have a file which is :
line1
line2
line3
What I am trying to have is
"line1"{
"line1"
}
I am trying to do this using awk but I don't know how to use the special characters. For now I have this.
awk '{ "$0" {"$0"} }'

$ awk '{$0="\""$0"\""; print $0 "{\n" $0 "\n}"}' file
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}

$ awk -v q='"' '{ql = q $0 q; print ql "{" ORS ql ORS "}" }' ip.txt
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
-v q='"' save the double quote character in variable named q, makes it easier to insert double quotes instead of messing with escapes
ql = q $0 q this adds double quotes around the input record
ql "{" ORS ql ORS "}" required output, ORS is output record separator which is newline character by default
space between the different parameters is ignored, use " }" to get a space before } in the output

as a comparison with sed
$ sed 's/.*/"&"{\n"&"\n}/' file
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}
also another awk
$ awk -v OFS="\n" -v q='"' '{v=q $0 q; print v "{", v, "}" }' file

You can simply use printf:
awk -v fmt='"%s"{\n"%s"\n}\n' '{printf fmt, $0,$0 }' file
Test with your data:
kent$ awk -v fmt='"%s"{\n"%s"\n}\n' '{printf fmt, $0,$0 }' f
"line1"{
"line1"
}
"line2"{
"line2"
}
"line3"{
"line3"
}

Related

awk - remove new line after printing all columns

i am running a following awk script
awk 'BEGIN { FS="|" ; OFS="|" }; { printf $0, $1 "_" $2 }' .someFile
unfortunatley the concatention of fields 1 and 2 is printed on new line, looks like the last field contains a new line character
how can i trim it ?
If you want to use printf (which may have been accidental), I think you can use this:
awk 'BEGIN { FS = OFS = "|" } { printf "%s%s%s_%s", $0, OFS, $1, $2 }' .someFile
printf should always be used with a format string. printf doesn't add the Output Record Separator to the end of what it prints, so you have to do that yourself using \n in the format string or by adding %s and passing ORS as the last argument to printf.
In this case, I think you can just use print though:
awk 'BEGIN { FS = OFS = "|" } { print $0, $1 "_" $2 }' .someFile

Using a field separator of the two-character string "\n" in awk

My input file has a plain-text representation of the newline character in it separating the fields:
First line\nSecond line\nThird line
I would expect the following to replace that text \n with a newline:
$ awk 'BEGIN { FS = "\\n"; OFS = "\n" } { print $1 }' test.txt
First line\nSecond line\nThird line
But it doesn't (gawk 4.0.1 / OpenBSD nawk 20110810).
I'm allowed to separate on just the \:
$ awk 'BEGIN { FS = "\\"; OFS = "\n" } { print $1, $2 }' test.txt
First line
nSecond line
I can also use a character class in gawk:
$ awk 'BEGIN { FS = "[[:punct:]]n"; OFS = "\n" } { $1 = $1; print $0 }' test.txt
First line
Second line
Third line
But I feel like I should be able to specify the exact separator.
A field separator is a type of regexp and when using a dynamic regexp you need to double escape everything:
$ awk 'BEGIN { FS = "\\\\n"; OFS = "\n" } { print $1 }' file
First line
See the man page for details.
Here sed might be a better tool for this task
sed 's/\\n/\n/g'

How to match new lines in awk?

Input file text.txt:
foo()
{
}
buz()
{
}
Awk script.awk:
BEGIN {
RS = "\n\n+";
FS = "\n";
}
/[a-z]+\(\)\n/ {print "FUNCTION: " $1;}
{print "NOT FOUND: " $0;}
Running script:
awk -f script.awk text.txt
gives:
NOT FOUND: foo()
{
}
NOT FOUND: buz()
{
}
But I've expected to match both functions WITH newlines. How to do this?
Since you're already using "\n" as the FS, you can just do matching against $1:
awk -v RS='\n\n+' -v FS='\n' '
$1 ~ /^[a-z]+\(\)$/ {print "FUNCTION: " $1; next}
{print "NOT FOUND: " $0}
' text.txt
This worked with gawk:
FUNCTION: foo()
FUNCTION: buz()
You can try this:
BEGIN {
RS = "";
FS = "\n";
}
/[a-z]+\(\)/ {print "FUNCTION: " $1;}
!/[a-z]+\(\)/ {print "NOT FOUND: " $0;}
If you want to verify that there is nothing after the () you can do this:
$1~/[a-z]+()$/ {print "FUNCTION: " $1;}
I don't know why newline isn't matched. Maybe someone would explain it.
This might work for you (GNU awk):
awk '{if(/^[a-z]+\(\)\n/)print "FUNCTION:"$1; else print "NOT FOUND: "$0}' RS="" file
Not sure what output you expect excactly.
In order to process FS in awk you need to indroduce a dummy command like $1=$1. Without it no filed parsing is done.
So if you expect result like this:
foo() { }
buz() { }
Just type:
awk '{$1=$1; print} ' RS='\n\n' FS='\n' OFS=" "

How to print out a specific field in AWK?

A very simple question, which a found no answer to. How do I print out a specific field in awk?
awk '/word1/', will print out the whole sentence, when I need just a word1. Or I need a chain of patterns (word1 + word2) to be printed out only from a text.
Well if the pattern is a single word (which you want to print and can't contaion FS (input field separator)) why not:
awk -v MYPATTERN="INSERT_YOUR_PATTERN" '$0 ~ MYPATTERN { print MYPATTERN }' INPUTFILE
If your pattern is a regex:
awk -v MYPATTERN="INSERT_YOUR_PATTERN" '$0 ~ MYPATTERN { print gensub(".*(" MYPATTERN ").*","\\1","1",$0) }' INPUTFILE
If your pattern must be checked in every single field:
awk -v MYPATTERN="INSERT_YOUR_PATTERN" '$0 ~ MYPATTERN {
for (i=1;i<=NF;i++) {
if ($i ~ MYPATTERN) { print "Field " i " in " NR " row matches: " MYPATTERN }
}
}' INPUTFILE
Modify any of the above to your taste.
The fields in awk are represented by $1, $2, etc:
$ echo this is a string | awk '{ print $2 }'
is
$0 is the whole line, $1 is the first field, $2 is the next field ( or blank ),
$NF is the last field, $( NF - 1 ) is the 2nd to last field, etc.
EDIT (in response to comment).
You could try:
awk '/crazy/{ print substr( $0, match( $0, "crazy" ), RLENGTH )}'
i know you can do this with awk :
an alternative would be :
sed -nr "s/.*(PATTERN_TO_MATCH).*/\1/p" file
or you can use grep -o
Something like this perhaps:
awk '{split("bla1 bla2 bla3",a," "); print a[1], a[2], a[3]}'

awk - how to specify field separator as binary value 0x1

Is it possible to specify the separator field FS in binary for awk?
I have data file with ascii data fields but separated by binary delimiter 0x1.
If it was character '1' it would look like this:
awk -F1 '/FIELD/ { print $1 }'
Or in script:
#!/bin/awk -f
BEGIN { FS = "1" }
/FIELD/ { print $1 }
How can I specify FS/F to be 0x1.
#!/bin/awk -f
BEGIN { FS = "\x01" }
/FIELD/ { print $1 }
See http://www.gnu.org/manual/gawk/html_node/Escape-Sequences.html.
awk -F '\x01' '/FIELD/ { print $1 }'
works on mawk, gawk, or nawk :
awk -F'\1' '{ … }'
awks can properly decipher the octal code without needing help from the shell like
awk -F$'\1' '{ … }'