~ / archivo / stackoverflow / 54353965
changing two lines of a text file
-
I have a bash script which gets a text file as input and takes two parameters (Line N° one and line N° two), then changes both lines with each other in the text. Here is the code:
#!/bin/bash awk -v var="$1" -v var1="$2" 'NR==var { s=$0 for(i=var+1; i < var1 ; i++) { getline; s1=s1?s1 "\n" $0:$0 } getline; print; print s1 s next }1' Ham > newHam_changed.txtIt works fine for every two lines which are not consecutive. but for lines which follows after each other (for ex line 5 , 6) it works but creates a blank line between them. How can I fix that?
- Michael 2019-01-24 19:31 Works good for me. Maybe the input file has
CR+LFline endings? - Miguel Ortiz yo 2019-01-24 20:10 Correction: Yes, it works with consecutive lines, when changing no consecutive lines the \n doesn't work.
- Barmar 2019-01-24 20:28 Please edit the question to describe the problem correctly. Imagine that comments can be deleted any time (because they can).
- Barmar 2019-01-24 20:30 Show a sample input and the output you're getting.
- Reza 2019-01-24 20:31 @Barmar if you say change line 10 anf 19 or anything like that it works fine. But if you want to change two consecutive line, for ex lines 11 and 12, it changes them but there will be a blank line between those. This is the problem
- Barmar 2019-01-24 20:35 Don't describe, show actual file contents. Add it to the question.
- Barmar 2019-01-24 20:37 @reza Sorry, I thought the comment above saying "Correction" was from you.
- Barmar 2019-01-24 20:44 I also can't reproduce the problem.
- Miguel Ortiz yo 2019-01-24 20:46 Added an alternative for this need in case someone looks for a way to perform the task in the future.
- Barmar 2019-01-24 20:46 I get incorrect output for non-consecutive lines. If I use 4 and 6, the output is
1 2 3 6 54 7 8 9(where those spaces are actually newlines) - Barmar 2019-01-24 20:51 I think you didn't post your actual script. I can fix my problem by changing it to
print s1 "\n" s, but when I do that I get the blank line for consecutive lines.
- Michael 2019-01-24 19:31 Works good for me. Maybe the input file has
-
I think your actual script is not what you posted in the question. I think the line with all the prints contains:
print s1 "\n" sThe problem is that when the lines are consecutive,
s1will be empty (theforloop is skipped), but it will still print a newline befores, producing a blank line.So you need to make that newline conditional.
awk -v var="4" -v var1="6" 'NR==var { s=$0 for(i=var+1; i < var1 ; i++) { getline; s1=s1?s1 "\n" $0:$0 } getline; print; print (s1 ? s1 "\n" : "") s next }1' Ham > newHam_changed.txt -
Using getline makes awk scripts always a bit complicated. It is better to prevent the use of getline and just make use of the awk
pattern { action }syntax. This will make perfectly readable scripts. In any other language you would just do a loop and get the next line, but in awk I think it is best to make good use of this feature.awk -v var="$1" -v var1="$2" ' NR==var {s=$0; collect=1; next;} NR==var1 {collect=0; print; printf inbetween; print s} collect {inbetween=inbetween""$0"\n"; next;} 1' HamHere I capture the first line in s when I found it and set the collect flag. This will trigger the collect block on the next iteration which collects all lines in between. Whenever the second line is found it sets the collect back to zero and prints first the current line, than the inbetween lines and then s. If the lines are consecutive inbetween is empty and printf will than do nothing.
- karakfa 2019-01-25 03:24
""not needed, but more importantlyprintf "%s", inbetween, for why try to print the string "asdfa%sasdf" withprintf.
- karakfa 2019-01-25 03:24
-
Too complex for my taste, here is something quite simple that achieves the same task:
#!/bin/bash ORIGFILE='original.txt' # original text file PROCFILE='processed.txt' # copy of the original file to be proccesed CHGL1=`sed "$1q;d" $ORIGFILE` # get original $1 line CHGL2=`sed "$2q;d" $ORIGFILE` # get original $2 line `cat $ORIGFILE > $PROCFILE` sed -i "$2s/^.*/$CHGL1/" $PROCFILE # replace sed -i "$1s/^.*/$CHGL2/" $PROCFILE # replaceMore code doesn't mean more useful, keep it simple. This code do not use
forand instead goes directly to the specific lines.EDIT:
A simple way on one line to do this task:
printf '%s\n' 14m26 26-m14- w q | ed -s fileFound in this answer.
- Poshi 2019-01-24 21:00 But this code have a problem: you are reading the file 5 times (and writing the answer three). This prevents you from piping the input and multiplies the processing time in a quite noticeably way for big files.