Awk OFS issues

Hi, Could anyone tell me what Im doing wrong here any help will be much appreciated

#!/bin/bash
ls -ltr /export/home/tjmoore > /export/home/tjmoore/log100
awk -F " " /export/home/tjmoore/log100 'BEGIN {OFS="\t";} {print $1,$2,$3,$4,$5,
$6,$7,$8,$9;}' > /export/home/tjmoore/log1001

I am getting this error message

awk: can't open BEGIN {OFS="\t";} {print $1,$2,$3,$4,$5,$6,$7,$8,$9;}
 record number 51

The awk script should appear before the file/s.

awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4,$5,
$6,$7,$8,$9}' /export/home/tjmoore/log100 > /export/home/tjmoore/log1001

Hi, thankyou for the help but Im still getting the below error message after trying your suggestion

awk: can't open BEGIN {OFS="\t";} {print $1,$2,$3,$4,$5,$6,$7,$8,$9;}

Still it is same as Elixir's code...

try

awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9}' OFS="\t" /export/home/tjmoore/log100 > /export/home/tjmoore/log1001
1 Like

Please post (exactly) what you typed in.

Hmm, is awk an alias or a wrapper script? What does

type awk

say?

Try like same way......

---------- Post updated at 06:38 AM ---------- Previous update was at 06:38 AM ----------

ls -ltr /export/home/tjmoore/log100 |awk '{print $1"\t"$2 }'>/export/home/tjmoore/log1001 

Hi everyone thanks for all the help!

type awk

returns

awk is /usr/bin/awk

I currently have this and it seems to be working! However I can't seem to get the OFS to equal a "tab" character, however way I do it with quotes it will interpret \t literally and use \t as the OFS.
Any suggestions?

ls -ltr /export/home/tjmoore > /export/home/tjmoore/log100
awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9}' OFS="\t" /export/home/tjmoore/log100 >
/export/home/tjmoore/log1001

---------- Post updated at 08:49 AM ---------- Previous update was at 08:43 AM ----------

hi bmk, Ive tried what you suggested and it works great, how would I get this to run through the entire file, as currently it prints the 1st and 2nd field of the first line then stops

Assuming you are printing all the lines of ls -ltr with tab spacing...

ls -ltr /export/home/tjmoore | awk '{$1=$1}1' OFS="\t" >> /export/home/tjmoore/log1001

If "\t" is not getting reflected. then also try with /usr/bin/awk

If still "\t" is not working then try with " " .:slight_smile:

1 Like

Thankyou to everyone, I've ended up using bmk's method and managed to get it to work, thats for all your help much appreciated! :slight_smile: