1836838 Members
2239 Online
110110 Solutions
New Discussion

Re: Awk Help

 
SOLVED
Go to solution
Ash_4
Advisor

Awk Help

Hi Masters,
I am having file containing numbers
23654
234444
23444

I want the output like below,
insert '23654'
insert '234444'
insert '23444'


In short, how can i print ' in awk.
Thanks in adv
13 REPLIES 13
John Meissner
Esteemed Contributor

Re: Awk Help

cat file | awk '{print "'"$1"'"}'
All paths lead to destiny
harry d brown jr
Honored Contributor
Solution

Re: Awk Help

John,

you mean:

cat file | awk '{print "insert '"$1"'"}'

or faster:

sed "s/.*/insert \'&1\'/" < filename

live free or die
harry
Live Free or Die
Leif Halvarsson_2
Honored Contributor

Re: Awk Help

Hi,
Or with shell script:

while read a
do
echo "insert '"$a"'"
done
John Meissner
Esteemed Contributor

Re: Awk Help

Harry - yeah... i guess... i didn't realize that they wanted the "insert" in the statement as well.
All paths lead to destiny
Tom Jackson
Valued Contributor

Re: Awk Help

Hi:

This works:

awk ' { printf "\x027insert\x027%s\n",$0 } ' inputfilename

Tom
H.Merijn Brand (procura
Honored Contributor

Re: Awk Help

Harry's sed problem is best, though it does not need the escapes for the quotes, nor does it need the redirection:

l1:/tmp 102 > cat > xx
23654
234444
23444
l1:/tmp 103 > sed "s/.*/insert '&1'/" xx
insert '236541'
insert '2344441'
insert '234441'
l1:/tmp 104 >
Enjoy, Have FUN! H.Merijn
Carlos Fernandez Riera
Honored Contributor

Re: Awk Help

Oh Oh!
it should be :
sed "s/.*/insert \'&\'/"


because &1 append a '1' on each line.
unsupported
harry d brown jr
Honored Contributor

Re: Awk Help


John,

I'm not sure the author will reply back.

Carlos,
You're right, I mangled the sed, here's one that works (and I took Procura's advice and removed the escaping of the apost's):


sed "s/\(.*\)/insert '\1'/" filename


live free or die
harry
Live Free or Die
Ash_4
Advisor

Re: Awk Help

Hi,
Extremly sorry for delay.
If have two field like below
293874 239487
239478 239487
----------
i want output as using "SED" or AWK
'293874' , '239487'
'239478' , '239487'
Christian Gebhardt
Honored Contributor

Re: Awk Help

Hi

echo "this is the trick" | awk '{printf("'\''%s'\''\n",$0)}'

in your case:
# cat inputfile
293874 239487
239478 239487
# awk '{printf("'\''%s'\'','\''%s'\''\n",$1,$2)}' inputfile
'293874','239487'
'239478','239487'

very clear, isn't it? ;-)

Chris
Christian Gebhardt
Honored Contributor

Re: Awk Help

uuups, with my browser i cannot read the quotes exactly, please try copy and paste and you will see:
quote-backslash-quote-quote to print ' in awk

Chris
H.Merijn Brand (procura
Honored Contributor

Re: Awk Help

Why not perl? Does it have to be sed or awk? Perl is so much fun!

Here's some obfuscated examples:

l1:/tmp 121 > cat file
12345
23456 78901
34567 89012 62534
45678 90123 72635 91827
l1:/tmp 122 > perl -ple '$"=",";$_=qq{@{[map{qq{\x27$_\x27}}split/\D+/]}}' file
'12345'
'23456','78901'
'34567','89012','62534'
'45678','90123','72635','91827'
l1:/tmp 123 > perl -nle 'print+v39,join(v39.44.39,split/\D+/),v39' file
'12345'
'23456','78901'
'34567','89012','62534'
'45678','90123','72635','91827'
l1:/tmp 124 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Awk Help

sed -e "s/$/ /" -e "s/\([0-9]*\)[[:space:]]/'\1',/g" -e "s/'',//g" -e "s/',$/'/" filename


Works like this:

(1) The first "-e"

-e "s/$/ /"

Appends a space to the end of the line (so the next test will always work - hacking)

(2) the second "-e"
-e "s/\([0-9]*\)[[:space:]]/'\1',/g"

Says match all number groups (numbers seperated by spaces, then wrap them with apost's and a comma)

(3) the third "-e"

-e "s/'',//g"

removes any BOGUS '', stuff

(4) the forth "-e"

-e "s/',$/'/"

Removes other BOGUS ', at the end of the line


live free or die
harry
Live Free or Die