根据匹配的字符串分割文件

时间:2020-02-11 02:19:00

标签: shell unix awk grep

我只有一个文件(input.txt),如下所示:

# STOCKHOLM 1.0

#=GF AC   RF00001
#=GF ID   5S_rRNA 
ghgjg---jkhkjhkjhk

## STOCKHOLM 1.0

#=GF AC   RF00002
#=GF ID   6S_rRNA

hhhjkjhk---kjhkjhkj


## STOCKHOLM 1.0

#=GF AC   RF00005
#=GF ID   12S_rRNA

hkhjhkjhkjuuwww

我必须分割该行等于## stockholm1.0的文件,并在第二个字符串RF00001_full.txt中用值命名该文件。因此,对于输入文件,我应该能够获得3个不同的文件,如下所示:

RF00001_full.txt

# STOCKHOLM 1.0

#=GF AC   RF00001
#=GF ID   5S_rRNA 
ghgjg---jkhkjhkjhk

RF00002_full.txt

## STOCKHOLM 1.0

#=GF AC   RF00002
#=GF ID   6S_rRNA

hhhjkjhk---kjhkjhkj

RF00005_full.txt

## STOCKHOLM 1.0

#=GF AC   RF00005
#=GF ID   12S_rRNA

hkhjhkjhkjuuwww

我到目前为止尝试过的代码如下:

while read p;
if [[ $p == ## STOCKHOLM 1.0* ]];
then
#what should I do here to sort the line by OS ? 

done <input.txt

1 个答案:

答案 0 :(得分:2)

请尝试使用提供的示例进行后续的,书面的和测试的操作。

awk '
/STOCKHOLM/{
  close(file)
  file=count=""
}
(/STOCKHOLM/ || !NF) && !file{
  val=(val?val ORS:"")$0
  count++
  next
}
count==2{
  count=""
  file=$NF"_full.txt"
  if(val){
    print val > (file)
    val=""
  }
  next
}
file{
  print >> (file)
}
' Input_file

说明: 在此处添加详细说明。

awk '                             ##Starting awk program from here.
/STOCKHOLM/{                      ##Checking condition if string STOCKHOLM is present in line then do following.
  close(file)                     ##Closing the file opened in background to avoid errors.
  file=count=""                   ##Nullifying variables file and count here.
}
(/STOCKHOLM/ || !NF) && !file{    ##Checking condition if line has string STOCKHOLM OR null fields AND file variable is NULL then do following.
  val=(val?val ORS:"")$0          ##Creating val which is concatenating its own value each time cursor comes here.
  count++                         ##Increment variable count with 1 here.
  next                            ##next will skip all further statements from here.
}
count==2{                         ##Checking condition if count is 2 then do following.
  count=""                        ##Nullifying count here.
  file=$NF"_full.txt"             ##Creating outputfile name here with last field and string adding to it.
  if(val){                        ##Check if val is NOT NULL then do following.
    print val > (file)            ##Printing val into output file here.
    val=""                        ##Nullifying val here.
  }
  next                            ##next will skip all further statements from here.
}
file{                             ##if file is NOT NULL.
  print >> (file)                 ##Printing lines into output file here.
}
' Input_file                      ##Mentioning Input_file name here.