Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要更新linux中最后一行以固定字符串开头的少数文件的最后一行中的子字符串_Linux_Grep - Fatal编程技术网

需要更新linux中最后一行以固定字符串开头的少数文件的最后一行中的子字符串

需要更新linux中最后一行以固定字符串开头的少数文件的最后一行中的子字符串,linux,grep,Linux,Grep,我需要更新最后一行以特定字符串开头的所有文件中字符串的第一个管道和第二个管道之间的文本。请帮我找到linux命令 文件abc.txt包含以下数据: Name|city|salary xyz|pun|450000 Footer|355|02052019|895 目录中还有其他文件,但我只需要更新最后一行以页脚开头的文件。要更新的文本是第一次和第二次出现的|之间的文本,不确定输出应该是什么,但如果第一个字段是页脚,这会将第二个字段更改为一些新字段 另一种方法是使用sed替换第一个“|”之后的第一组

我需要更新最后一行以特定字符串开头的所有文件中字符串的第一个管道和第二个管道之间的文本。请帮我找到linux命令

文件abc.txt包含以下数据:

Name|city|salary
xyz|pun|450000
Footer|355|02052019|895

目录中还有其他文件,但我只需要更新最后一行以页脚开头的文件。要更新的文本是第一次和第二次出现的|

之间的文本,不确定输出应该是什么,但如果第一个字段是页脚,这会将第二个字段更改为一些新字段


另一种方法是使用sed替换第一个“|”之后的第一组数字的值。您可以使用sed替换形式sed'/locate/s/find/replace/'文件,在该文件中查找以^Footer开头的行,并查找|[0-9][0-9]*并将其中的数字替换为您喜欢的任何数字,例如| 322而不是355

您可以通过向sed提供-i选项来进行适当的更改。对你来说,这看起来像:

$ sed -i '/^Footer/s/|[0-9][0-9]*/|322/' file
以及更改后生成的数据文件:

$ cat file
Name|city|salary
xyz|pun|450000
Footer|322|02052019|895

有几种方法可供选择。

首先切换到工作目录

初始文件如下:

Name|city|salary
xyz|pun|450000
Footer|355|02052019|895
脚本从这里开始:

srch="Footer"   # keyword to search at last of file.

for file_name in `ls`   # iterate all files in current directory
    do
    last_line_first_word="`tail -1 $file_name | cut -d "|" -f 1-1`"   # first word of the last line

    if [ "$srch" == "$last_line_first_word" ]   # if last word match proceed with replace
        then
        awk -F "|" '{$2="replace_string"; print; }' $file_name > dummy.txt  # replace second column with user string
        sed -i 's/ /|/g' dummy.txt   # replace <spaces> with "|" 
        sed -i '' 's/ /|/g' dummy.txt  # replace <spaces> with "|" [ In MAC ]
        mv dummy.txt  $file_name        # rename to original filename
        echo "File modified ->> "$file_name
    fi
    echo "File NOT modified "$file_name
done
希望这有帮助

日志:


欢迎来到StackOverflow。如中所述,此站点是有用问题及其答案的存储库,而不是教程站点或帮助论坛。请参加,访问,特别是阅读和学习如何有效地使用本网站。
srch="Footer"   # keyword to search at last of file.

for file_name in `ls`   # iterate all files in current directory
    do
    last_line_first_word="`tail -1 $file_name | cut -d "|" -f 1-1`"   # first word of the last line

    if [ "$srch" == "$last_line_first_word" ]   # if last word match proceed with replace
        then
        awk -F "|" '{$2="replace_string"; print; }' $file_name > dummy.txt  # replace second column with user string
        sed -i 's/ /|/g' dummy.txt   # replace <spaces> with "|" 
        sed -i '' 's/ /|/g' dummy.txt  # replace <spaces> with "|" [ In MAC ]
        mv dummy.txt  $file_name        # rename to original filename
        echo "File modified ->> "$file_name
    fi
    echo "File NOT modified "$file_name
done
Name|replace_string|salary
xyz|replace_string|450000
Footer|replace_string|02052019|895
File NOT modified Applications
File NOT modified Desktop
File NOT modified Documents
File NOT modified Downloads
File NOT modified IdeaProjects
File NOT modified Library
File NOT modified Movies
File NOT modified Music
File NOT modified Pictures
File NOT modified Public
File NOT modified PycharmProjects
File modified ->> test.txt
File NOT modified test.txt