Unix Commands – How to Remove String After Last Underscore in Filenames

awkfilenamessedtext processing

I want to edit the file contents in the 2nd row and 2nd column ,which is a file name from that I want to remove the part after the last underscore and before the file extension .

The contents of the file looks like below
Input file contents

No|filename|count
01 |com_101_00000_0001_a234.txt|100

resultant file content

No|filename|count  
01 |com_101_00000_0001.txt|100

The number of underscores may vary depending upon the file names present inside the .ctl files.there are multiple files in a folder likes this ,i want to change the contents for all .ctl file in the 2nd row similarly.

Operating system details Linux  2.6.32 -696.30.1.e16
GNU/linux

Best Answer

Try

awk -F\| -vOFS=\| 'NR==2 {sub (/_[^_]*\./,".", $2)}1' file
No|filename|count
01 |com_101_00000_0001.txt|100


NR==2       if record count is 2 (i.e. 2. line)
sub (       `awk` "substitute" function
/_[^_]*\./  regex: match string consisting of multiple non-underscores, delimited by leading underscore and trailing literal dot
,".",       with a literal dot
 $2)        in second field
1           do default action: print

Redirect > into temp file, and mv / cp back to original file if desired. For multiple files, run in a for loop for all relevant files.

Related Question