Filename Manipulation in AWK


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Filename Manipulation in AWK
# 1  
Old 06-02-2009
Filename Manipulation in AWK

I have created a nawk program and passing a file as an input parameter.
The filename will be /home/dir/ksh/test.out

I need to extract the filename alone. Is there anyway to get this ?

Input : /home/dir/ksh/test.out
Output -1: test.out
Output -2 : t

Input : /home/dir/kshfiles/file.out
Output -1: file.out
Output -2 : e

And from this output I need to extract the particular 4th character from the last. The filename and the directorynamewill get vary each and every time. So I need to manipulate from the last.
# 2  
Old 06-02-2009
You can do it with sed:
Code:
input=/home/dir/kshfiles/file.out
output1=`echo $input | sed 's;.*/;;g'`
output2=`echo $output1 | sed 's/.*\([0-9a-z]\)\..\{3\}/\1/' `

hope that helps.

Last edited by thanhdat; 06-02-2009 at 09:45 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk manipulation

Hello all, Can someone help me with write part of code in awk to merge 2 files? Go through file1 check if number from column 3 exist in file2(column 2) if yes take value from column 1 and add to column 4 in file1. If value in column 4 exist in file1 skip it. file1... (2 Replies)
Discussion started by: vikus
2 Replies

2. Shell Programming and Scripting

Array manipulation with awk?

Dear friends, I'm wondering if we could do some simple math on two arrays with the same size? a1 Fe -0.21886700 -0.01417600 -0.24390300 C 2.20529400 0.89434100 -0.61061000 C -1.89657700 -0.74793000 -0.07778200 C ... (8 Replies)
Discussion started by: liuzhencc
8 Replies

3. Shell Programming and Scripting

awk manipulation

hello I have example file AA 11 BB 22 CC 33 And what I expect to have -a AA=11 -a BB=22 -a CC=33 can anyone help how I have this using awk? (1 Reply)
Discussion started by: vikus
1 Replies

4. Shell Programming and Scripting

awk manipulation

Hallo Family, I have csv file which has over a million records in it. All i want to do is to change field 2 to have the same value as field 10. sample file:Now 0860093239,Anonymous,unconditional,+27381230283,Anonymous,unconditional,y,public,,2965511477:0A Desired output: ... (2 Replies)
Discussion started by: kekanap
2 Replies

5. Shell Programming and Scripting

Filename manipulation

Hi all, I need to convert some files with the following name format: <Base File Name>_YYYYMMDD_HHMISS_NNNNN.* to: NNNNNN_<Base File Name>_YYYYMMDD_HHMISS.* Example: MY_FILE_NAME_20120912_123443_12345.data will be converted to 012345_MY_FILE_NAME_20120912_123443.data Notice that... (9 Replies)
Discussion started by: schweinshaxe
9 Replies

6. UNIX for Dummies Questions & Answers

File manipulation via awk

Hello, I am having issues generating the output file below from this input file: Basically, what I want is if the ID= matches with the line below to print the first value in column 3 and the last value of column 4 for the matching ID's. The ID's can repeat more than twice, however, they... (2 Replies)
Discussion started by: verse123
2 Replies

7. Shell Programming and Scripting

filename manipulation using "sed"

Dear all, I need to manipulate some filenames (dump.1, dump.2, etc.) and feed them to another command. For this purpose I am using sed and because my last COMMAND needs to receive files one-by-one I am using xargs: >> ls dump.* | xargs sed -n 's/expression1/expression2/' | COMMAND The... (4 Replies)
Discussion started by: bigboss010
4 Replies

8. Shell Programming and Scripting

$0 manipulation in awk

OK, so if $0 represent the entire record... can I change $2 and will that be reflected back in $0? I think the following answers that YES, it does work. But is there anything I should be thinking about prior to doing this? What I am actually doing is part of 5 pages of scripting and awk... (1 Reply)
Discussion started by: joeyg
1 Replies

9. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

10. Shell Programming and Scripting

awk manipulation

Hi , what a wonderful command but so hard to maintain ! i have a file like that : 03/07/2006 05:58:45 03/07/2006 06:58:45 03/07/2006 07:58:50 03/07/2006 08:58:50 and i want to read it and keep only the lines with 3rd field less than 07:00:00 writing it in a second file ! ... (2 Replies)
Discussion started by: Nicol
2 Replies
Login or Register to Ask a Question