awk processing / Shell Script Processing to remove columns text file

Tags
cut -d, ls, programming

 
Thread Tools Search this Thread
# 1  
Old 03-23-2013
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
Code:
 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 :

Code:
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 ?
# 2  
Old 03-23-2013
Here's one way using awk:
Code:
awk '{print substr($0,7,length($0)-6)}' filelist

Output:
Code:
AAAI94-018.pdf
Acoustic instruments.pdf
Ground-Truth Transcriptions .pdf

Or if you want to stick with cut command:
Code:
cut -c7- filelist


Last edited by mjf; 03-23-2013 at 10:56 AM..
# 3  
Old 03-23-2013
Code:
cut -c/- filelist

? (UUOC)
# 4  
Old 03-23-2013
Another approach:
Code:
awk '{sub($1,x);$1=$1}1' filelist

# 5  
Old 03-23-2013
Thanks

Hello,

Thank you for helping me out. It works..Smilie

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

Hello,

So I ran this Awk code in the end

Code:
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,

Code:
[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
Code:
[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 ?
# 6  
Old 03-23-2013
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.

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk for text processing biofreek Shell Programming and Scripting 3 05-22-2018 08:12 AM
How to log file processing details to database table usnig UNIX shell script? Atul kumar Shell Programming and Scripting 3 02-20-2016 12:56 PM
Text columns processing using awk dovah Shell Programming and Scripting 4 07-17-2014 10:05 AM
Text processing using awk dovah Shell Programming and Scripting 3 07-17-2014 02:23 AM
Text processing of file ajayram Shell Programming and Scripting 6 01-26-2012 05:34 AM
AWK: Remove spaces before processing each line? kevintse Shell Programming and Scripting 6 08-23-2010 10:23 PM
File Processing in shell script telangmadhuri Shell Programming and Scripting 7 05-04-2010 07:59 AM
Awk text processing zam Shell Programming and Scripting 8 01-12-2010 08:11 PM
2 file processing script in C shell jclanc8 Shell Programming and Scripting 1 12-28-2009 05:24 PM
awk, perl Script for processing a single line text file hmsadiq Shell Programming and Scripting 1 04-12-2009 04:44 PM
KSH script -text file processing NULL issues geauxsaints Shell Programming and Scripting 2 10-01-2008 05:10 PM
text processing ( sed/awk) clx Shell Programming and Scripting 12 02-29-2008 08:44 AM
text file processing alias47 UNIX for Dummies Questions & Answers 1 08-09-2007 12:10 PM
Have a shell script check for a file to exist before processing another file heprox Shell Programming and Scripting 3 11-14-2006 03:26 AM
Processing a text file TheCrunge UNIX for Dummies Questions & Answers 1 11-09-2005 11:47 AM