changing two lines of a text file

python change specific line in text file
java read text file line by line
java program to read a text file and write to another file
how to change a line in a file with python
change a specific line in file python
how to open a text file in java
how to read a text file in java
python replace line in file in place

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.txt 

It 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?

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" s

The problem is that when the lines are consecutive, s1 will be empty (the for loop is skipped), but it will still print a newline before s, 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 

How to multi-line "Replace in files" in Notepad++, strings. You have to make sure to select "Extended" search mode in the bottom left corner of the search window. A new file is opened in write mode named "copy.c". Now the contents of original file are transferred into new file and the line to be modified is deleted. New data is stored in its place and remaining lines of the original file are also transferred. The copied file with modified contents is replaced with the original file's name.

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' Ham

Here 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.

How to edit a specific line in a text file in Python, How do you edit a line in a text file in Python? This makes the lines random and can be useful for programming applications. For example, you could want to shuffle the lines of a text file randomly and creat a new file. The file could have several thousands of lines. See the example. Input data; This is line one. This is line two. This is line three. This is line four. Output: This is line two.

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 # replace

More code doesn't mean more useful, keep it simple. This code do not use for and 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 file

Found in this answer.

How to write multiple lines in text file using Python?, How do you write multiple lines in a text file in Python? 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 " " $0:$0 } getline; print; print s1 s next }1' Ham > newHam_changed.txt

changing two lines of a text file, How do I find and replace text in a file in Python? Although we can’t directly search and replace text inside a text file, we can do the next best thing. We can: 1) open up a text file; 2) read the text into a variable; 3) do a search-and-replace on that variable; and 4) re-save the text file. We can even do all that from the command line, although we’ll hold off on that for a moment.

How can I change multiple lines in a text file?, 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" s. The problem is that  Original Text File Content: Do the dishes0 Feed the dog0 Cleaned my room1. Output: Do the dishes0 Feed the dog0 Cleaned my room1 -----Do the dishes1 Feed the dog0 Cleaned my room1. New text file content: Do the dishes1 Feed the dog0 Cleaned my room1

java for complete beginners - reading text files, For each material in the a text file I need to change a value, 2.5e8, to a random variable and I would like to generate many verions of the file  Once that is understood, it should be clear that replacing a line in a text file involvescreating a copy of the original file by writing all lines up to the one which is to bereplaced, then write the replacement line followed by all lines from the original filewhich follow the line being replaced.

Comments
  • Works good for me. Maybe the input file has CR+LF line endings?
  • Correction: Yes, it works with consecutive lines, when changing no consecutive lines the \n doesn't work.
  • Please edit the question to describe the problem correctly. Imagine that comments can be deleted any time (because they can).
  • Show a sample input and the output you're getting.
  • @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
  • "" not needed, but more importantly printf "%s", inbetween, for why try to print the string "asdfa%sasdf" with printf.
  • 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.