Home > Enterprise >  Read specific text with bash and save to .txt
Read specific text with bash and save to .txt

Time:10-07

How can I read after . up to __ and save in a new .txt file with bash?

File

 2021-01-12-1530.HUIG__002                                                 
 2021-01-12-1530.MEIG__002                                                 
 2021-01-12-1530.PCIG__002                                                 
 2021-01-12-1530.PEIG__002                                                 
 2021-01-12-1530.PNIG__002                                                 
 2021-01-12-1530.TGIG__002                                                 
 2021-01-12-1530.TOIG__002                                                 
 2021-01-12-1530.TPIG__002                                                 
 2021-01-12-1530.TXIG__002                                                 
 2021-01-12-1530.UXUV__002                                                 
 2021-01-12-1530.CCIG__002                                                 
 2021-01-12-1530.CMIG__002                                                 
 2021-01-12-1530.OXIG__002                                                 
 2021-01-12-1530.PLIG__002                                                 
 2021-01-12-1530.TUIG__002                                                 
 2021-01-12-1530.YAIG__002                                                 
 2021-01-12-1530.ZIIG__002                                                 
 2021-01-12-1530.FTIG__002                                                 
 2021-01-12-1530.NEUV__002                                                 
 2021-01-12-1530.TLIG__002                                                 
 2021-01-12-1530.YOIG__002                                                 
 2021-01-12-1530.DAIG__002                                                 
 2021-01-12-1530.HLIG__002                                                 

CodePudding user response:

This works:

$ head file1.txt 
2021-01-12-1530.HUIG__002                                                 
2021-01-12-1530.MEIG__002                                                 
2021-01-12-1530.PCIG__002                                                 
2021-01-12-1530.PEIG__002                                                 
2021-01-12-1530.PNIG__002                                                 
2021-01-12-1530.TGIG__002                                                 
2021-01-12-1530.TOIG__002                                                 
2021-01-12-1530.TPIG__002                                                 
2021-01-12-1530.TXIG__002                                                 
2021-01-12-1530.UXUV__002
$ cut -d. -f2 file1.txt | cut -d_ -f1 | head
HUIG
MEIG
PCIG
PEIG
PNIG
TGIG
TOIG
TPIG
TXIG
UXUV

And saving to a new file:

cut -d. -f2 file1.txt | cut -d_ -f1 > file2.txt

CodePudding user response:

With just bash (i.e. not GNU utils):

while read -r line;
do
    tmp="${line##*.}"
    echo "${tmp%%_*}"
done < file.txt > file_2.txt

(one liner)

while read -r line; do tmp="${line##*.}"; echo "${tmp%%_*}"; done < test.txt > file2.txt

CodePudding user response:

With sed(1)

sed 's/^.*\.//;s/__.*$//' file.txt > new_file.txt

With GNU grep(1) which has support for PCRE

grep -Po '(?<=\.).*(?=__.*)' file.txt > new_file.txt
  • Related