将一个print语句附加到文件中的每一行,前一行作为输出

时间:2022-01-26 13:53:23

In troubleshooting a reboot crash I need to print every line in a file to an output console. What I want to do is run a linux command line or script to take each line in a file, and put a print statement after it containing the line, so for example:

在故障检修中,我需要将文件中的每一行打印到输出控制台。我要做的是运行一个linux命令行或脚本来获取文件中的每一行,并在包含该行的行之后加上一个print语句,例如:

while x<100
    x=x+1
end while

would become

将成为

while x<100
print "while x<100"
    x=x+1
    print "x=x+1"
end while
print "end while"

even better would be if the print statements could include the line number:

如果打印语句可以包括行号,那就更好了:

while x<100
print "Line 50: while x<100"
    x=x+1
    print "Line 52:x=x+1"
end while
print "Line 54: end while"

The idea here is to find the last line in the script that executes as that will be the line that probably caused the device to reboot. Currently I do this manually, and it is rather time consuming to add all those lines.

这里的想法是在脚本中找到执行的最后一行,因为它可能会导致设备重新启动。目前我是手工操作的,添加所有这些行非常耗时。

I'm aware of the existence of SED and AWK but have never used them, I do use ex/vi/grep/ls/wc fairly often to sort things out in text files though and have experience with basic shell scripting.

我知道SED和AWK的存在,但是我从来没有使用过它们,但是我经常使用ex/vi/grep/ls/wc来对文本文件进行排序,并且有使用基本shell脚本的经验。

EDIT: Note that none of the code in my question is a shell script. I'm looking for help with creating a shell script or a command line to process a file so that each line in the file is followed by a print statement containing the previous line.

编辑:请注意,我的问题中的任何代码都不是shell脚本。我正在寻找帮助,创建一个shell脚本或命令行来处理一个文件,以便文件中的每一行都遵循包含前面一行的打印语句。

2 个解决方案

#1


2  

awk '{printf("%s\nprint \"Line %s : %s\"\n",$0,++NR, $0)}' infile > outfile

while x<100
print "Line 2 : while x<100"
    x=x+1
print "Line 4 :     x=x+1"
end while
print "Line 6 : end while"

$0 is content of each line, NR is line/record number.

$0是每行内容,NR是行/记录号。


for the lines that already have quotes and print statements, use the below modified awk command:

对于已经有引号和打印语句的行,使用以下修改后的awk命令:

awk '{printf("%s\nprint \"Line %s : ",$0,++NR)} {gsub(/\"/,"\\\"");printf $0"\"\n"}' infile > outfile

#2


1  

Well, a basic Bash shell script would be

一个基本的Bash shell脚本是。

while read -r line
do
  echo "$line"
  printf "print \"%q\"\n" "$line"
done

This uses only bash builtins and is meant to be placed in a script file (typing it in at the terminal may confuse the read command). It currently takes input from stdin, but a redirection operator such as command < file.txt will fix that.

这只使用bash内置程序,并将其放置在脚本文件中(在终端上输入可能会混淆read命令)。它目前接受来自stdin的输入,但是是一个重定向操作符,比如command < file。三种可以纠正这个问题。

Also note the use of the %q format specifier in the call to printf, it makes sure that the resulting output can be passed back into the shell to get the same text out of it.

还要注意在对printf的调用中使用的%q格式说明符,它确保结果输出可以返回到shell中,以从shell中获得相同的文本。

#1


2  

awk '{printf("%s\nprint \"Line %s : %s\"\n",$0,++NR, $0)}' infile > outfile

while x<100
print "Line 2 : while x<100"
    x=x+1
print "Line 4 :     x=x+1"
end while
print "Line 6 : end while"

$0 is content of each line, NR is line/record number.

$0是每行内容,NR是行/记录号。


for the lines that already have quotes and print statements, use the below modified awk command:

对于已经有引号和打印语句的行,使用以下修改后的awk命令:

awk '{printf("%s\nprint \"Line %s : ",$0,++NR)} {gsub(/\"/,"\\\"");printf $0"\"\n"}' infile > outfile

#2


1  

Well, a basic Bash shell script would be

一个基本的Bash shell脚本是。

while read -r line
do
  echo "$line"
  printf "print \"%q\"\n" "$line"
done

This uses only bash builtins and is meant to be placed in a script file (typing it in at the terminal may confuse the read command). It currently takes input from stdin, but a redirection operator such as command < file.txt will fix that.

这只使用bash内置程序,并将其放置在脚本文件中(在终端上输入可能会混淆read命令)。它目前接受来自stdin的输入,但是是一个重定向操作符,比如command < file。三种可以纠正这个问题。

Also note the use of the %q format specifier in the call to printf, it makes sure that the resulting output can be passed back into the shell to get the same text out of it.

还要注意在对printf的调用中使用的%q格式说明符,它确保结果输出可以返回到shell中,以从shell中获得相同的文本。