awk processing / Shell Script Processing to remove columns text file | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

awk processing / Shell Script Processing to remove columns text file

Programming


Tags
cut -d, ls

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-23-2013
ajayram ajayram is offline
Registered User
 
Join Date: May 2011
Last Activity: 8 July 2014, 3:26 PM EDT
Posts: 35
Thanks: 3
Thanked 0 Times in 0 Posts
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 ?
Sponsored Links
    #2  
Old 03-23-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 23 October 2014, 7:58 AM EDT
Location: Newtown, PA
Posts: 123
Thanks: 7
Thanked 29 Times in 28 Posts
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 09:56 AM..
Sponsored Links
    #3  
Old 03-23-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 4:22 AM EDT
Location: Aachen, Germany
Posts: 4,421
Thanks: 73
Thanked 1,081 Times in 1,019 Posts

Code:
cut -c/- filelist

? (UUOC)
    #4  
Old 03-23-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
Another approach:

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

Sponsored Links
    #5  
Old 03-23-2013
ajayram ajayram is offline
Registered User
 
Join Date: May 2011
Last Activity: 8 July 2014, 3:26 PM EDT
Posts: 35
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks

Hello,

Thank you for helping me out. It works..

---------- 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 ?
Sponsored Links
    #6  
Old 03-23-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK: Remove spaces before processing each line? kevintse Shell Programming and Scripting 6 08-23-2010 09:23 PM
File Processing in shell script telangmadhuri Shell Programming and Scripting 7 05-04-2010 06:59 AM
2 file processing script in C shell jclanc8 Shell Programming and Scripting 1 12-28-2009 04:24 PM
awk, perl Script for processing a single line text file hmsadiq Shell Programming and Scripting 1 04-12-2009 03:44 PM
KSH script -text file processing NULL issues geauxsaints Shell Programming and Scripting 2 10-01-2008 04:10 PM



All times are GMT -4. The time now is 05:30 AM.