views:

51

answers:

3

Hi guys I want to print in awk line and character ' after the line, but awk put the character in the middle of the line. Here is my code :

awk -v d="'" {print $0 d}

i am using bash2 thanks for help

A: 

Try:

 awk -v d="'" '{print $0 d }'

Note the single quotes around the command.

The MYYN
well i posted just simplified code but as i see it i wrote the whole command :
Deecome
awk -v s="$s" -v d="'" 'length == max2 {printf "%s %d %d %s", s , FNR , max2 , $0}' max2=$max file s is some string. I want to add the d after the $0
Deecome
Can you add some (small) sample input to your question? Would make it easier to comprehend.
The MYYN
the point is that the awk print longest line(s) from file. max2 is length of longest line. I want to print "Output: " (that is s) then line number that is FNR , then length of the line , then the line and i want to end it with '.
Deecome
It seems like The MYYN's answer should work even in the more complex case. Note that simply placing the "d" next to the "$0" invokes the implicit awk concatenation operator. Should work fine...
DigitalRoss
but it isnt working. when i get whole print into another '' END FILE error appear
Deecome
@Deecome: Based on your comment above, this prints any line that matches the length of the longest so far, then updates that `max2` length when a longer line is found: `awk -v d="'" -v s="$s" 'length > max2 {max2=length} length == max2 {printf "%s %d %d %s%s\n", s, FNR, max2, $0, d}'`.
Dennis Williamson
thanks Dennis. but I have max length, i dont have to find it in awk but i will try your printf
Deecome
it didnt solved my problem $0 is the line i want to print and after it the d which is ' but it insert character into the line. It placed it in the beginning of last line in output (line could be too long to display into single stdout line) and replace the character that should be there
Deecome
a cant see logic in placing the character in the beginning of the line. Maybe it is too much variables for printf or i dont know. The thing is if my terminal window is smaller i need more lines to display output so it put ' elsewhere. it hasn't fixed position
Deecome
A: 

Use dos2unix on your file, and try the command again.

Or do something like this

awk -v d="'" '{gsub(/[[:space:]]/,"");print $0 d }' file
ghostdog74
but i cant change the file
Deecome
A: 

Finally i get it work. With sub(/\r$/,"") befoure print thanks for help

Deecome