awk help

hi guys
I need some help.

I have this file which has records like

-rw-r--r--    1 xxxxxxxx xxxxxxx      23552 Mar  3  2010 this is a test.txt 
-rw-r--r--    1 xxxxxxxx xxxxxxx      24064 Mar  3  2010 this  test2   .txt
-rw-r--r--    1 xxxxxxxx xxxxxxx      28160 Aug 17 11:12 this is test 2.txt

i have this line of code to get the files names out

files=`cat sftp_log_xxx_am_1102021458.log | awk '{ $1=$2=$3=$4=$5=$6=$7=$8=""; print $0 "~" }' `

however, instead of getting

"this  test2   .txt~"

i get

"this test2 .txt~"
  • it truncates the spaces. I need to maintain them!

any help guys?

awk '{for(i=1;i<=8;i++){$i=""};}1' file
1 Like

Gives this as output:

$ awk '{for(i=1;i<=8;i++){$i=""};}1' file             
        this is a test.txt
        this test2 .txt
        this is test 2.txt
$

and also truncates the spaces.

How about:

awk '{sub(".*" $8 FS,x)}{print $0 "~"}' file
1 Like

Through sed..

sed 's/.*[0-9][0-9]\+ \(.*\)/\1~/' inputfile > outfile
1 Like
cut -c58- file
1 Like

many thanks for your help.

I have tried this:

cat xxx.log | awk '{sub(".*" $8 FS,x)}{print $0 "~"}'
awk: syntax error near line 1
awk: illegal statement near line 1

but get the error? have id done something wrong

Try /usr/xpg4/bin/awk

1 Like

thanks for the reply

however, i am sorry to say it does not solve my original problem

cat xxx.log

-rw-r--r--    1 xxxx sftponly        6 Feb  2 14:51 this_is_a_test_file1.txt
-rw-r--r--    1 xxxx sftponly        6 Feb  2 14:51 this is  a test      file2.txt
drwxr-xr-x    2 xxxx sftponly      512 Feb  2 15:04 test
-rw-r--r--    1 xxxx sftponly       13 Feb  3 09:53 this_is_a_new_test1.txt
-rw-r--r--    1 xxxx sftponly       13 Feb  3 09:53 thisisanewtest1.txt
sftp>

run this

cat xxx.log | sed "1,/^sftp> ls -ltr/d" | /usr/xpg4/bin/awk '{for(i=1;i<=8;i++){$i=""};}1'

get this

        this_is_a_test_file1.txt
        this is a test file2.txt
        test
        this_is_a_new_test1.txt
        thisisanewtest1.txt

i was expected e.g

this_is__a_test______file2.txt

have i implemented the solution incorrectly?

Try:

/usr/xpg4/bin/sed 's/\([^ ]* *\)\{8\}//;s/$/~/' infile
1 Like

Have you tried this?

/usr/xpg4/bin/awk '{sub(".*" $8 FS,x)}{print $0 "~"}' file
1 Like

you should Franklin's solution.

nawk  '{sub(".*"$8 FS,"");gsub(" ","_")}1' file
1 Like

Hi,

Another solution using 'gawk':

$ awk --version | head -1
GNU Awk 3.1.8
$ awk --posix 'BEGIN { FS="([[:graph:]]+[[:blank:]]+){8}" } { printf "%s\n", $2 }' infile
this is a test.txt 
this  test2   .txt
this is test 2.txt

Regards,
Birei

1 Like

hi guys got it working i think.

 
|/usr/xpg4/bin/awk '{sub(".*" $8 FS,x)}{print $0 }' 
 

That seemed to do the trick.

Thanks for all the help