2

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:

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.

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.

alphablender
  • 2,168
  • 5
  • 27
  • 42
  • It's important to know what language your code IS in, to know how to escape the data in the print statement when your line of code already contains quotes. – Russell Reed Dec 21 '14 at 02:47
  • 1
    reading your purpose with the answer, to me it seems that you try to instrument your script instead of properly debugging it. an approach likes [this](http://stackoverflow.com/questions/21190771/how-to-properly-debug-a-bash-script) to use `set -xv` or `extdebug`, looks more feasible. – lp_ Dec 21 '14 at 10:43

2 Answers2

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.


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

awk '{printf("%s\nprint \"Line %s : ",$0,++NR)} {gsub(/\"/,"\\\"");printf $0"\"\n"}' infile > outfile
αғsнιη
  • 2,627
  • 2
  • 25
  • 38
  • Thanks, your answer exposed some flaws in my thinking as well, now I need to figure out how to handle lines that already have quotes and print statements, possibly just adding a " in front of the : might work – alphablender Dec 22 '14 at 20:24
  • 1
    @alphablender Updated answer for handling the lines if already have quotes. – αғsнιη Dec 23 '14 at 11:47
1

Well, a basic Bash shell script would be

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.

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.

randomusername
  • 7,927
  • 23
  • 50
  • the output isn't clean and appears to be in reverse order, and lines are smushed together, also, every output line has a print statement on it, not every other line – alphablender Dec 22 '14 at 20:17
  • @alphablender see my edit, I added a newline character to the ``printf`` function to fix it. – randomusername Dec 23 '14 at 04:01
  • You also need to add double quotes to `echo "$line"` because otherwise any metacharacters in `line` are going to be interpreted by the shell (redirection, wildcard, variable expansion ... havoc). Similarly you should use `read -r` to provide, eherm, less "useful" semantics for `read`. – tripleee Dec 23 '14 at 04:56
  • @tripleee true, I've added those in – randomusername Dec 23 '14 at 04:59
  • As I said in my first comment, the output isn't clean, by that I mean, it is full of backslashes in between words: txtfer=createobject("rourltransfer") print "txtfer=createobject\(\"rourltransfer\"\)" Pasting into Stack Overflow comment box I lose some of the backslashes, so you can't see how bad it is unfortunately, but it is quite severe – alphablender Dec 27 '14 at 03:10