Sponsored Content
Top Forums UNIX for Beginners Questions & Answers awk to print filename words along with delimiter Post 303011333 by Don Cragun on Tuesday 16th of January 2018 08:06:56 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
mbtg(1) 						      General Commands Manual							   mbtg(1)

NAME
MBTG - Memory Based Tagger generator SYNOPSYS
mbtg -T <filename> -s <setting filename> or mbtg [options] DESCRIPTION
This programs generates, based on a tagged corpus, all the files needed to be able to tag a text with mbt. OPTIONS
-h or --help show help -T <tagged training corpus file> or -E <enriched tagged training corpus file> All further options have reasonable defaults, so using them is only needed for the experienced user. See the mbt manual for more details. -s settingsfile mbtg creates this file, which can be used to run mbt with minimal effort. (like mbt -s settings -T somefile) -p pattern the pattern for known words (default ddfa) -P pattern the pattern for unknown words (default dFapsss) -% <number> filter threshold for ambitag construction (default 5%) -l <lexiconfile> -L <file with list of frequent words> -r <ambitagfile> -k <known words case base> -u <unknown words case base> -K <known words instances file> -U <unknown words instances file> -V or --version show version info -e <sentence delimiter> (default '<utt>') -X keep the intermediate files -Otimbl options (Note: there is NO SPACE between O and the options) <options> classifier options for both known and unknown words instances bases K: <options> classifier options for known words instance base U: <options> classifier options for unknown words case base valid timbl options are: a d k m q v w x - BUGS
possibly AUTHORS
Ko van der Sloot Timbl@uvt.nl Antal van den Bosch Timbl@uvt.nl SEE ALSO
timbl(1) mbt(1) mbtserver(1) 2011 march 21 mbtg(1)
All times are GMT -4. The time now is 04:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy