awk to print filename words along with delimiter


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk to print filename words along with delimiter
# 1  
Old 01-16-2018
awk to print filename words along with delimiter

Hi,
I have filename as:
Code:
010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937

I need first 5 words of my filename along with the respective delimiters:
I tried this:

Code:
f=010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937
echo $f | awk -F '[_-]' '{print $1$2$3$4$5}'
010020001SFORSortSYEXC

But i want output with delimiters like:
Code:
010020001_S-FOR-Sort-SYEXC

I dont want to add those delimiters in print manually because _,- is not fixed i may get all delimiters as _ or - or both.
Is there any way to print first 5 words of filename along with the delimiters?

TIA
# 2  
Old 01-16-2018
Hello gnnsprapa,

Could you please try following and do let me know if this helps you.
Code:
awk 'END{split(FILENAME, array,"_");print array[1] array[2]}' 010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937

Thanks,
R. Singh
# 3  
Old 01-16-2018
thanks for suggesting the solution: I got this

Code:
010020001S-FOR-Sort-SYEXC

its missing the first underscore(_) between first 2 words of filename
# 4  
Old 01-16-2018
Hello gnnsprapa,

Apologies, in hurry I missed it. Please try following and let me know if this helps you.
Code:
awk 'END{split(FILENAME, array,"_");print array[1] "_"  array[2]}' 010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-16-2018
Try also shell parameter expansion
Code:
echo ${f%[-_]${f#*[_-]*[-_]*[_-]*[_-]*[_-]*}}
010020001_S-FOR-Sort-SYEXC

This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-16-2018
@RudiC
while trying this.i got output as
Code:
f=010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937
echo ${f%[-_]${f#*[_-]*[-_]*[_-]*[_-]*[_-]*}}
010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937

I dont want words after 5th word of filename .i.e i want only
Code:
010020001_S-FOR-Sort-SYEXC

@Ravinder
as i told i dont want to hardcore any delimiter, in your solution

Code:
awk 'END{split(FILENAME, array,"_");print array[1] "_"  array[2]}' 010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937

i will get correct result but if my filename has "-" instaed of "_" then also it will give me "_" as i have hardcoded it:
for eg: i have 2 files as
Code:
f=010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937
f1=010020001-S-FOR-Sort-SYEXC_20180109_062320_0100.x937

i want output as
Code:
010020001_S-FOR-Sort-SYEXC
010020001-S-FOR-Sort-SYEXC

appreciate your help so far, thanks
# 7  
Old 01-16-2018
What's your shell version? Does it offer "parameter expansion / Remove matching prefix/suffix pattern"?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Print filename and last line using awk

Hi, using awk command I want to print filenames and the last line of each file, in a single command line statement. I want to use 'awk', because I want to add more functionality to this logic later. I tried the following on *.sh files in the current directory find . -type f -name "*.sh"... (26 Replies)
Discussion started by: ysrini
26 Replies

3. Shell Programming and Scripting

Print filename with awk

I can got the filename with this script. it's only show "-" in result. cut -d , -f7 CSV_d.* | awk 'OFS=":"{print FILENAME,substr($1,1,8),substr($1,9,2),substr($1,11,2),substr($1,13,2)}' | sort |uniq (2 Replies)
Discussion started by: before4
2 Replies

4. Shell Programming and Scripting

print number of words in each line

Hi, Please suggest a way to print number of words in the end of each line. <input file> red aunt house blue sky bat and ball game <output file> red aunt house 3 blue sky 2 bat and ball game 4 Thanks! (2 Replies)
Discussion started by: mira
2 Replies

5. Shell Programming and Scripting

perl or awk print strings between words

hi everyone, 1.txt 981 I field1 > field2.a: aa, ..si01To:<f:a@a.com>From: <f:a@a.com>;tag=DVNgfRZBZRMi96 <f:a@1:333>;ZZZZZ: 12345 the output field1 field2 <f:a@a.com> the output is cut the string 3rd and 5th field, and get the value betwee "To:" and "From:", please advice. ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

6. Shell Programming and Scripting

Print only matching words

Hi All, I have searched the forum and tried to print only matching(pattern) words from the file, but its printing entire line. I tried with grep -w. I am on sunsolaris. Eg: cat file A|A|F1|F2|A|F3|A A|F10|F11|F14|A| F20|A|F21|A|F25 I have to search for F (F followed by numbers) and ... (5 Replies)
Discussion started by: gsjdrr
5 Replies

7. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

8. Shell Programming and Scripting

print only last two words of a line

can u help me out to print last two words of each sentence of a file. for example. contents of input file: i love songs my favourite songs sent songs all kind good buddy Ouput file should contain: love songs favourite songs sent all kind good buddy (5 Replies)
Discussion started by: pradeepreddy
5 Replies

9. Programming

How do I print(save) words to postscript?

It should be pretty simple but I'm just new to IDL and am workling through the command line and scripts(program.pro). If I want to save words to the bottom of a .ps file that I am putting a few plots in what should written in my code. set_plot works for plots but apparently not words. Print... (0 Replies)
Discussion started by: luked
0 Replies

10. 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
Login or Register to Ask a Question