awk Print NF Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Print NF Question
# 1  
Old 09-30-2015
awk Print NF Question

Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables A_394857_figure.pdf

First I apologize for my novice question. I'm still learning sed & awk.

I have a file with the following data format:
Code:
Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables/A_394857_figure.pdf

This file contains over 8000 data lines in which the path may be more or less then what is shown. I've been requested to do a simple format (or so I thought) on the file to look like the following:
Code:
Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables	A_394857_figure.pdf

There is a tab that replaces the last slash character which will seperate the last field.

To start, The first awk line I've used to do this is:
awk -F"/" '{$NF=""}1'

I know this kills the last $NF, but again, I'm just learning this (getting what I can from the Sed & Awk, O'Reilly book), and am trying to separate the filename from the path of a single line.

I've searched through numerous threads, and even though I know there is a solution available, I couldn't find one.

Your help and explanations would be greatly appreciated!
# 2  
Old 09-30-2015
Hi, this can be done with sed:
Code:
sed 's|\(.*\)/|&      |' file

The TAB is a hard-tab, entered with CTRL-V TAB

--
Some sed's can use a \t code, but this is a non-standard extension, for example GNU sed:
Code:
sed 's|\(.*\)/|&\t|' file

--
With awk, you could do this:
Code:
awk -F/ '{sub("/[^/]*$", "\t" $NF)}1' file


Last edited by Scrutinizer; 09-30-2015 at 02:19 PM..
# 3  
Old 09-30-2015
How about
Code:
awk -F"/" '{sub (FS $NF "$", "\t" $NF)}1' OFS="/"  file
Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables    A_394857_figure.pdf

# 4  
Old 09-30-2015
WOW, that did it. I never thought of using sed.

Many Many Thanks for your help! Much appreciated!!
# 5  
Old 09-30-2015
Small correction to Scrutiizer's proposal (which does NOT replace the last slash):
Code:
sed 's|\(.*\)/|\1	|' file
Petro_Project/Regional Work/Charge Access/Studies_PG2014/P_Tables    A_394857_figure.pdf

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-30-2015
I checked yours too RudiC. It worked and I'm checking on some of your usage in the book...

Thank You for your help!
# 7  
Old 09-30-2015
Yes it should be \1 rather than & of course :roll eyes:

--
Just one note about the use of awk. RudiC and my approach will give the same result in most cases.
However, if $NF is used in the matching part (the first field) of the sub() function and it contains special regular expression characters then that may give undesired results.
The first field is interpreted as ERE, an extended regular expression (unless it consists of a single character in double quotes)

There is no problem with using $NF in the replacement part of the sub() function (the 2nd field), since that is a string.

Last edited by Scrutinizer; 09-30-2015 at 03:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk question - print columns with names

I found this command and would like to know what it means: gawk 'NR==1{for(i=1;i<=NF;i++)if($i~/FE/)f=i}{for(i=0;i<n;i++)printf"%s%s",i?" ":"",$f;print""}' It seems to mean if the row =1 assign i to that row, and then if FE is in the top row /column then increment and print the row. I am... (2 Replies)
Discussion started by: newbie2010
2 Replies

2. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

5. Shell Programming and Scripting

awk print question

Hello, I have an input file like 123,456,789,321,654,987,IN,OUT,2012,000075,CF34EQ It has 11 fields separated by comma. I am trying to do this while read string do var=$(echo $string | awk -F"," '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}') set -- $var echo $1 ... ... echo $10... (3 Replies)
Discussion started by: apenkov
3 Replies

6. Shell Programming and Scripting

echo/print variable question

while read filer ; do echo $filer $1 $2; ssh $filer vfiler status -r | awk '/running/{host=$1}/Path:/{path=$2;print host,path}'; done < filers.list this will print node1 vfiler0 / vfiler2 /vol/vfiler2_vol0 vfilert /vol/vfiler_vol vfilert /vol/virt_vol where node1 = $filer. however how... (1 Reply)
Discussion started by: riegersteve
1 Replies

7. Shell Programming and Scripting

Perl question - print last element in a hash?

I am modifying someone else's code. There is a foreach statement printing the contents of a hash. Can someone give me an example of printing the last element in a hash? The output currently is A B C D E I want the output to be E (1 Reply)
Discussion started by: streetfighter2
1 Replies

8. Shell Programming and Scripting

Question about AWK and print

Hello friends, can any one tell what is meaning of this awk '{print NR ". " $0}' phonebook i just want to know what is ". " and $0 mean in this. thanks (2 Replies)
Discussion started by: tarunsavalia
2 Replies

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question