How to separate fields with space or tab in awk
Solution 1
Just change the line to
ls -la >> a.txt ; awk {'print $5 " " $1'} a.txt ;
this should print the output with spaces.
Hope this helps.
Edit:
As suggested by McNisse you can use printf, which would provide you good output format
ls -la >> a.txt ; awk {'printf ("%5s\t%s\n", $5, $1)'} a.txt ;
Solution 2
Another awk-specific technique, use the "output field separator"
ls -la | awk -v OFS='\t' '{print $5, $1}'
The comma is crucial here.
Solution 3
A simple way to get tabs is:
awk {'print $5"\t"$1'}
Solution 4
I know this is an old thread, but I'm just learning and found these posts helpful. My best solution was to use gawk to insert spaces between the variables for you.
ls -la | gawk '{print $1, $9}'
Solution 5
To place the space between the arguments, just add " "
, e.g. awk {'print $5" "$1'}
.
However it is not recommended to parse output of ls
command, since it's not reliable and output is for humans, not scripts. Therefore use alternative commands such as find
or stat
.
Here is example using GNU stat
:
$ stat -t *
001.txt 23 8 81a4 501 20 1000004 242236402 1 0 0 1460260387 1460260239 1460260239 1460260194 4096
7c1c.txt 21 8 81a4 501 20 1000004 242236595 1 0 0 1460261322 1460260486 1460260486 1460260486 4096
which will print you machine-friendly output (in terse form), so you can get exactly what you need. Then use -c
to use specific format, or use awk
, cut
or read
to get the right columns.
Check stat --help
for further options. For example to print day of modification, check this example.
mx7
LinkedIn Profile: https://in.linkedin.com/in/raja-genupula-05205531 Endorse me If I helped you. Blog : http://thelinuxmen.blogspot.com/ Ubuntu - CentOS - Fedora - Windows - Severs - IIS - FTP - Security Thank you.
Updated on September 18, 2022Comments
-
mx7 over 1 year
While playing with
awk
I came to execute:ls -la >> a.txt ; awk {'print $5 $1'} a.txt ;
This is giving output like:
53277-rw------- 52347-rw-------
How can I get a space between these two friends of output?
-
Admin over 11 yearsIn awk, to concatenate two strings, you just place them side-by-side --
print $5 $1
-
Admin about 8 years@kenorb and close voters, the linked duplicate is not a duplicate at all. They know already how to print fields by premise (
awk {'print $5 $1'} a.txt
). They're asking how to put a space in between field #1 and field #5.
-
-
McNisse over 11 yearsThe
printf
function provides better control, specially if you want to format numbers.printf("%s\t%s\n", $5, $1)
-
muru over 9 years-1: Nothing new.
gawk
,mawk
, or any otherawk
, they all insert spaces if you use the comma, which is what this answer says. -
Nick Crawford over 8 yearsShould be
ls -la | awk '{print $1, $9}'
. As muru saidgawk
isn't necessary. -
loretoparisi over 7 yearsthis works but cause
$1
to go to newline -
Vishal over 4 yearsIs there a typo in above answer? what does
5s
mean inawk {'printf ("%5s\t%s\n", $5, $1)'}
-
mchid over 4 yearsIt appears the OP is trying to parse the output for human reading purposes and not for the purposes of running a script (like
for i in ls; do $i
). -
twan163 over 4 years@Vishal - if you insert a number after the % sign, then you specify a field width, in this case 5. The output will be right aligned. If you enter a negative number, the output will be aligned on the left.
-
daknowles about 4 yearsThanks I hate it. But slightly less than the other options.
-
fiatux over 2 years@loretoparisi, no it doesn't. There are likely to be
\r
characters at your end-of line.