1
I would like to use AWK to remove the first 8 columns from the following output:
ls -l
lrwxrwxrwx 1 user user 23 jul 27 00:04 file1.pdf
-rw-rw-r-- 1 user user 107 may 8 13:59 file 2 with spaces.mp3
lrwxrwxrwx 1 user user 11 jul 24 19:43 file3-with-hyphens.txt
lrwxrwxrwx 1 user user 11 jul 24 19:43 and_another_file4_with_underscores.md
-rw-rw-r-- 1 user user 107 may 8 13:59 file 5 with way more spaces than the rest.mp3
and send the result to a text file. I can do that manually in vim with visual block select, however I would prefer to have a script to do it automatically.
Looking around I was able to find this page where, by changing the relevant parts of the 9th example and piping the output to itself as many times as required, I was able to get the desired result, but I feel that there must be a better (more elegant and/or compact) way to do it but haven't been able to find it or come up with my own.
My final code is the following:
awk '{for(i=1;i<=NF;i++)if(i!=x)f=f?f FS $i:$i;print f;f=""}' x=1 ~/file_folder_content.txt | awk '{for(i=1;i<=NF;i++)if(i!=x)f=f?f FS $i:$i;print f;f=""}' x=1 | ... | awk '{for(i=1;i<=NF;i++)if(i!=x)f=f?f FS $i:$i;print f;f=""}' x=1 >> ~/file_folder_content.txt
Note 1: I expect the actual output from the ls
command to be much larger than this.
Note 2: I tried to print the 9th field, but since some file names contain spaces, this only prints the first word in the file name.
1Remove the first 8 columns? That output only has 8 columns. Are you trying to just print the filenames?
ls -1
does that. – confetti – 2018-08-31T05:55:02.867Trial and error left me with this ridiculous command:
ls -l | tail -n +2 | column -tdN test -o "|" | cut -d "|" -f 9
- It prints only the 9th column. There must be a better way but hey, it works, lol. – confetti – 2018-08-31T06:03:45.530