awk processing / Shell Script Processing to remove columns text file

Hello,

I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this :

This is the output of ls command : I stored the output in a file filelist

 1.1M AAAI94-018.pdf
 188K Acoustic instruments.pdf
1000K Ground-Truth Transcriptions .pdf

I would like to just get the filenames, so I need to filter this output . I tried the cut command like this :

cat filelist | cut -f 1 -d ' '

but it does not work because of varying file sizes. like the last line of the output does not have a space in the beginning.

Also problematic is that there are spaces within the filenames..

So I need to remove the first 7 columns of every line in this output. that will just give me the full name of each file.
How do I do it ?

Here's one way using awk:

awk '{print substr($0,7,length($0)-6)}' filelist

Output:

AAAI94-018.pdf
Acoustic instruments.pdf
Ground-Truth Transcriptions .pdf

Or if you want to stick with cut command:

cut -c7- filelist 
cut -c/- filelist

? (UUOC)

Another approach:

awk '{sub($1,x);$1=$1}1' filelist

Hello,

Thank you for helping me out. It works..:slight_smile:

---------- Post updated at 07:03 PM ---------- Previous update was at 06:00 PM ----------

Hello,

So I ran this Awk code in the end

awk '{print substr($0,7)}' filelist

through the shell like this : I wanted the output to get redirected to the same file as the input
thus,

[milli@milli ~]$ sh test.awk > filelist

I expected to see the filelist overwritten with the new input from the Results of the Shell Script, however, it produces a blank file.

If I give this command

[milli@milli ~]$ sh test.awk >> filelist

Then it gets appended as usual.

If I try a different filename for output redirection , then it works properly.

Any idea what is happening ?

When shell encounters a redirection operator > the file will be truncated first (0 byte size) and then opened for writing, so after the redirection file is opened for writing, then shell starts test.awk process. This is the reason why you get a blank output file.

But in case of append >> the previous data is retained and then shell starts test.awk process. This is the reason why the data gets appended.