Visit Our UNIX and Linux User Community


AWK end of line question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK end of line question
# 1  
Old 01-09-2008
AWK end of line question

Any clues on how to parse a line returned from an ls command that allows for the filename to be fully passed even if it includes spaces? What I got close with is:
ls -ltra | awk '{print $1 "|" $3 "|" $4 "|" $5 "|" $6 "|" $7 "|" $8 "|" $9 $10 $11 $12 ... (etc)}'
However this clears the spaces in the filename and metls the name... Basically anything beyond the 9th field can be lumped into one variable for full printing or whatever...

Any other suggestions?

Thanks!
# 2  
Old 01-10-2008
you can use for loop inside awk starting from field 9, since file names from ls -ltr output starts at field 9
eg
Code:
.... | awk '{
  for ( i=9 ; i<=NF;i++) {
  # find some way to concat to become file name
 } 
}'

and unless you want to do some other things with the various columns of ls -l , you can do away with -l (or use -1 ) instead to just get the file name.
Code:
ls -tra | ...... 
or
for files in *
do
  # do something with "$files"
done

# 3  
Old 01-10-2008
Thanks, the for loop cleans up the processing as does using OFS for the output delimiter. The problem is still the filename or at least being able to determine the starting location of the filename for use in a substr operation....

any thoughts?
# 4  
Old 01-10-2008
U can try something like this:
Code:
ls -ltra| awk '{
                    for (i=1;i<=NF;i++)
                       {
                       if ( i > 8 )
                          printf("%s ",$i)
                       else
                          printf("%s|",$i)
                       }
                       printf("\n")
                    }'

# 5  
Old 01-10-2008
Quote:
Originally Posted by Klashxx
U can try something like this:
Code:
ls -ltra| awk '{
                    for (i=1;i<=NF;i++)
                       {
                       if ( i > 8 )
                          printf("%s ",$i)
                       else
                          printf("%s|",$i)
                       }
                       printf("\n")
                    }'

Yee hah!!! We have a winner!!!

Thanks Klashxx
# 6  
Old 01-10-2008
Quote:
Originally Posted by jpport123
Any clues on how to parse a line returned from an ls command that allows for the filename to be fully passed even if it includes spaces? What I got close with is:
ls -ltra | awk '{print $1 "|" $3 "|" $4 "|" $5 "|" $6 "|" $7 "|" $8 "|" $9 $10 $11 $12 ... (etc)}'
However this clears the spaces in the filename and metls the name... Basically anything beyond the 9th field can be lumped into one variable for full printing or whatever...

Any other suggestions?

Thanks!
Code:
touch "this is a file with spaces in  it"
ls -1 this* | [n]awk '{print "\""$0"\""}'

# 7  
Old 01-11-2008
This worked great for returning just the name, but doesn't work so well with an ls -ltra for instance to show me owner and other information.

I did like the enclosed quoting - that will come in handy I'm sure...

Thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #560
Difficulty: Medium
Computers do exactly as they are told to do, except when they are an A.I.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Add column to end of each line with ´awk´

Hi, I have data with approximately 300 columns. I want to add a column to the end of each column with the value "1". Is there a way that I can do this is ´awk´ without having to specify each individual column. For instance, my data looks like: pvb 1 2 3 4 5 ....... 300 fdh 3 4 5 2 4 ......... (4 Replies)
Discussion started by: owwow14
4 Replies

3. Shell Programming and Scripting

awk to count start and end keyword in a line

Hello fellow awkers and seders: need to figure out a way to ensure a software deployment has completed by checking its trace file in which I can store the deployment results as follows: echo $testvar ===== Summary - Deploy Result - Start ===== ===== Summary - Deploy Result - End =====... (1 Reply)
Discussion started by: ux4me
1 Replies

4. Shell Programming and Scripting

awk - end of line - Help!

I have a textfile which I am parsing with awk. The lines do not have the same number of fields, so sometimes $3 is the last field, sometimes not. When I do a 'printf("%s, %s\n", $2, $3)', if $3 is the last field in the line, when I cat the file the output looks something like this:... (3 Replies)
Discussion started by: dmesserly
3 Replies

5. Shell Programming and Scripting

awk one liner to print to end of line

Given: 1,2,whatever,a,940,sot how can i print from one particular field to the end of line? awk -F"," '{print $2 - endofline}' the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens: 1---2---whatever---a---940---sot (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

awk Adding a Period at the end of a line

I started venturing in learning the art of using AWK/GAWK and wanted to simply added a period from line #11 to line #28 or to the end of the file if there is data. So for example: 11 Centos.NM 12 dojo1 13 redhat.5.5.32Bit 14 redhat.6.2.64Bit... (5 Replies)
Discussion started by: metallica1973
5 Replies

7. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

8. Shell Programming and Scripting

simple sed question - replace from phrase to end of line

I have the following line an in input file I want to digest with sed and simple replace the bold part with a variable defined in my bash script. I can do this in several sed operations but I know there must be a way to do it in a single sed line. What is the syntax? Line in file:... (1 Reply)
Discussion started by: graysky
1 Replies

9. Shell Programming and Scripting

awk : delete ^M at the end of the line

Hi all, I'm newbi in scripting. could someone tell how to delete the ^M at the end of the linie with an awk command. many thanks in advance. (2 Replies)
Discussion started by: liliput
2 Replies

Featured Tech Videos