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
# 8  
Old 01-16-2018
my shell version:
Code:
echo $BASH_VERSION
4.1.2(1)-release

# 9  
Old 01-16-2018
Quote:
Originally Posted by gnnsprapa
my shell version:
Code:
echo $BASH_VERSION
4.1.2(1)-release

Then it should work:
Code:
$ f=010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937
$ echo "${f%_*_*_*}"
010020001_S-FOR-Sort-SYEXC
$ echo "${f%[-_]*[-_]*[-_]*}"
010020001_S-FOR-Sort-SYEXC
$ echo "${f%[-_]"${f#*[_-]*[-_]*[_-]*[_-]*[_-]*}"}"
010020001_S-FOR-Sort-SYEXC

Are you sure that it was executed in that bash4 shell?
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 01-16-2018
The following should give you what you want even if the filename contains white-space characters:
Code:
printf '%s\n' "$f" | awk -F'[-_]' '{print substr($0, 1, length($1$2$3$4$5)+4)}'

Note that echo on many systems will interpret some sequences of characters as escape sequences and perform various transformations that you don't want. Using printf as shown above instead of echo avoids that issue.

Scrutinizer,
I think gnnsprapa is saying that any of the underscores (not just the first one) could be hyphens instead of underscores. Your suggestion assumes that the number of words stored in the variable is a constant (which has not been stated) and that the last five words are separated by underscores only.

As long as the given value assigned to the variable f contains at least six words, the following should work:
Code:
f_tail=${f#*[-_]*[-_]*[-_]*[-_]*[-_]}
printf '%s\n' "${f:1:$((${#f} - ${#f_tail} - 2))}"

with his version of bash or a recent ksh. The awk suggestion above should work even if there are only five words in the value assigned to f and should work with any shell using Bourne shell syntax.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 01-17-2018
Hey RudiC,Don,scrutnizer thanks a lot for your solutions...every solution worked so i am using

Code:
echo "${f%[-_]"${f#*[_-]*[-_]*[_-]*[_-]*[_-]*}"}"

It parse all the types of filename i receive, and also easy to understand. if possible can u make me understand this code:

Code:
printf '%s\n' "$f" | awk -F'[-_]' '{print substr($0, 1, length($1$2$3$4$5)+4)}'

Thanks, your solution worked every time for meSmilie
# 12  
Old 01-17-2018
The awk command extracts count characters (sum of lenghts of the first 5 fields) plus 4 field separators from $0, the unmodified input line, starting at the begin of line.
Try also
Code:
awk -vf=$f -F'[_-]' 'BEGIN {$0 = f; sub (FS $6 ".*$", ""); print}'
010020001_S-FOR-Sort-SYEXC

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