从文件开头剪切字符串并附加到Bash中的文件名

时间:2019-05-15 08:17:29

标签: bash

我想从文件开头剪切一定长度的字符串并将其附加到我的文件名中。是否有一个单行代码可以对整个文件目录执行此操作?

例如,我有文件1.txt,其中包含以下内容:

123456abcdefg12345678...

我要将文件重命名为1_123456.txt,并将内容编辑为:

abcdefg12345678....

我创建了一个bash脚本来解决此问题,如下所示:

for f in ./*; do
  a = $(head -c 8 $f)
  cut -c 9- > $f
  mv {,$a_}$f
done

但是,这并没有达到预期的效果-问题似乎出在mv行中。如果单线也能解决问题,我也将不胜感激。

2 个答案:

答案 0 :(得分:1)

$ echo 123456abcdefg12345678 > 1.txt
$ tail -c +7 1.txt > 1_$(head -c +6 1.txt).txt ; rm 1.txt . 
$ cat 1_123456.txt 
abcdefg12345678

答案 1 :(得分:0)

也许不是一个衬里(除非您将其放入shell脚本中),但是您可以尝试以下方法:

#!/bin/bash

# require two parameters:
# 1 the name of the input directory and
# 2 the name of the output directory
if [ $# -ne 2 ]
then
  echo usage
  exit 1
fi

INDIR=$1
OUTDIR=$2
CHAR_CNT=4                         # Number of chars to take from file content and insert into output file name

# Create the output directory just in case it does not exist.
mkdir -p $OUTDIR

# for each file.
for FILENAME in $INDIR/*
do
  if [ -f $FILENAME ]              # Make sure it is a regular file.
  then
    BASENAME=`basename $FILENAME`  # Extract just the file name.

    NAMEPART=${BASENAME%.*}        # From the file name, get just the name part.
    EXT=${BASENAME##*.}            # From the file name, get just the extension.
    PREFIX=`head -1 $FILENAME | cut -c 1-$CHAR_CNT`    # Get the first n characters from the input file.
    NEWFILE=$OUTDIR/${NAMEPART}_$PREFIX.$EXT   # Construct the output file name (and output path)

    # Degugging statements to show what is going on.
    #echo file: $FILENAME - NamePart: $NAMEPART, extension: $EXT, prefix = $PREFIX
    #echo new file name: $NEWFILE

    # Transfer the input file to the output, remove the first n characters from the file.
    # The output file will be named as per the format originalName_charsFromFile.extension
    sed "1s/^.\{$CHAR_CNT\}//" < $FILENAME > $NEWFILE
    echo "processed: $FILENAME -> $NEWFILE"
  else                             # Not a regular file.
    echo skipping $FILENAME
  fi
done

我将以上内容放入了名为renameSpecial.sh的脚本中。运行它时,结果如下所示。

gmc@linux-ihon:~/projects/so/renFil>   #### Starting point - input files
gmc@linux-ihon:~/projects/so/renFil> tree .
.
└── sampleData
    ├── file1.txt
    ├── file2.txt
    ├── testdir
    │   └── ignoredFile.txt
    ├── t.txt
    └── x.y.z.txt

2 directories, 5 files
gmc@linux-ihon:~/projects/so/renFil> cat sampleData/x.y.z.txt 
1234567890abcdefg
gmc@linux-ihon:~/projects/so/renFil> cat sampleData/t.txt 
12aaaa
34bbb
56cccc
gmc@linux-ihon:~/projects/so/renFil> cat sampleData/file2.txt 
abcdefg
gmc@linux-ihon:~/projects/so/renFil> cat sampleData/file1.txt
aaaaaaa
gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil>  #### Run the command
gmc@linux-ihon:~/projects/so/renFil> renameSpecial.sh 
usage: /home/glennm/bin/renameSpecial.sh inDir outDir
where:
  inDir    the name of a directory containing the input files
  outDir   the name of a directory that will contain the output files
           if outDir does not exist, this script will create it.

gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil> renameSpecial.sh sampleData outData
processed: sampleData/file1.txt -> outData/file1_aaaa.txt
processed: sampleData/file2.txt -> outData/file2_abcd.txt
skipping sampleData/testdir
processed: sampleData/t.txt -> outData/t_12aa.txt
processed: sampleData/x.y.z.txt -> outData/x.y.z_1234.txt
gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil> #### Result new directory with *new* file names
gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil> tree .
.
├── outData
│   ├── file1_aaaa.txt
│   ├── file2_abcd.txt
│   ├── t_12aa.txt
│   └── x.y.z_1234.txt
└── sampleData
    ├── file1.txt
    ├── file2.txt
    ├── testdir
    │   └── ignoredFile.txt
    ├── t.txt
    └── x.y.z.txt

3 directories, 9 files
gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil> ### Finally, the chars used in the file names are removed from the beginning of the file.
gmc@linux-ihon:~/projects/so/renFil> 
gmc@linux-ihon:~/projects/so/renFil> cat outData/x.y.z_1234.txt 
567890abcdefg
gmc@linux-ihon:~/projects/so/renFil> cat outData/t_12aa.txt 
aa
34bbb
56cccc
gmc@linux-ihon:~/projects/so/renFil> 

如果要“重命名”文件,只需在if语句底部附近的rm $FILENAME步骤之后添加sed

您说要重命名文件,我的示例在新位置创建了处理文件的新版本。这仍然可以满足您的需求,只需在开始之前重命名输入目录,或将输出文件复制回输入目录(例如,作为最后一步)。我只是这样做,因为它有助于调试,我可以运行它多次,而无需重新创建输入等。