Printing the invert of the last field of awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing the invert of the last field of awk
# 1  
Old 09-13-2007
Printing the invert of the last field of awk

in csh

set x = "/home/usr/dir1/file1"

if i do:

echo $x | awk -F\/ '{print $NF}'

will result to:
"file1"

how do i invert the output to:
"/home/usr/dir1"

Smilie
# 2  
Old 09-13-2007
Why use awk?

Code:
dirname $x

anyway to use awk:

Code:
echo $x|awk -F\/ '{for (i=1;i<=NF-2;i++)printf("%s/",$i);printf("%s\n",$i)}'


Last edited by Klashxx; 09-13-2007 at 07:38 AM..
# 3  
Old 09-13-2007
Quote:
Originally Posted by jehrome_rando
in csh

set x = "/home/usr/dir1/file1"

if i do:

echo $x | awk -F\/ '{print $NF}'

will result to:
"file1"

how do i invert the output to:
"/home/usr/dir1"
[...]
In csh:

Code:
% set x = "/home/usr/dir1/file1"
% echo $x:h
/home/usr/dir1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

awk - printing nth field based on parameter

I have a need to print nth field based on the parameter passed. Suppose I have 3 fields in a file, passing 1 to the function should print 1st field and so on. I have attempted below function but this throws an error due to incorrect awk syntax. function calcmaxlen { FIELDMAXLEN=0 ... (5 Replies)
Discussion started by: krishmaths
5 Replies

4. Shell Programming and Scripting

Printing uniq first field with the the highest second field

Hi All, I am searching for a script which will produce an output file with the uniq first field with the second field having highest value among all the duplicates.. The output file will produce only the uniqs which are duplicate 3 times.. Input file X 9 B 5 A 1 Z 9 T 4 C 9 A 4... (13 Replies)
Discussion started by: ailnilanjan
13 Replies

5. Shell Programming and Scripting

awk if statement not printing entire field

I have an input that looks like this: chr1 mm9_knownGene utr3 3204563 3206102 0 - . gene_id "Xkr4"; transcript_id "uc007aeu.1"; chr1 mm9_knownGene utr3 4280927 4283061 0 - . gene_id "Rp1"; transcript_id "uc007aew.1"; chr1 mm9_knownGene ... (5 Replies)
Discussion started by: pbluescript
5 Replies

6. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

7. Shell Programming and Scripting

printing the max of each field

hi guys im very new to scripting and i have a problem. i need to use awk in my script and the script needs to print the max for each of the columns in a file. for example: numbers.txt 10 15 20 30 40 58 25 30 15 10 38 10 38 8 9 ./max numbers.txt 58 25 38 30 40 i have no clue on how to... (4 Replies)
Discussion started by: youness
4 Replies

8. UNIX for Advanced & Expert Users

Printing Field with Delimiter in AWK/cut

Hello, I had posted earlier about printing fields using AWK, but now I have a slightly different problem. I have text files in the format: 1*2,3,4,5 and wish to print the first, third, and fifth fields, including the asterisk and commas. In other words, after filtering it should look... (1 Reply)
Discussion started by: Jahn
1 Replies

9. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies

10. Shell Programming and Scripting

Printing value with no obvious field seperator

Hi all, Can anybody think of a way to do this? I have a file with content like the following: F_TOP_PARAM_VALUEF_TOP_SOURCEF_TOP_DEL_NOTIFICATIONF_DEST_ADDRF_TOP_DEL_TYPE What I want to do is print out the value in the square brackets after F_TOP_SOURCE. So in this case I'd like to print... (4 Replies)
Discussion started by: Donkey25
4 Replies
Login or Register to Ask a Question