change the position of a line in a file using sed

how can i replace text after a specific word using sed?
sed replace line in file
sed replace character at position
sed replace string in file
sed replace string in file with variable
sed insert text in middle of line
man sed
bash replace line in file

I would like to know how to change the position of a line in a file (preferably using sed). For example, consider the file that contains

goal identifier statement  
let statement 1  
let statement 2  
forall statement  
other statements

I would like to be able to do this

goal identifier statement  
forall statement  
let statement 1  
let statement 2  
other statements  

where I change the position of the forall line and bring it after the goal line. forall and goal are regexps that can be used to identify the lines.


you can try, for move line 4 to line 2, I want to move line A to line B, where A>B

sed -n '2{h; :a; n; 4{p;x;bb}; H; ba}; :b; p' file

or A<B

sed -n '2{h; d}; 4{p; x;}; p' file

you get, in first case: move line 4 to line 2

goal identifier statement
forall statement
let statement 1
let statement 2
other statements

you get, in second case: move line 2 to line 4

goal identifier statement  
let statement 2  
forall statement  
let statement 1  
other statements

Explanation

sed -n '              #silent option ON
    2{                #if is line 2
        h             #Replace the contents of the hold space with the contents of the pattern space
        :a            #label "a"
        n             #fetch the next line
        4{            #if is line 4
            p         #print line 4
            x         #Exchange the contents of the hold and pattern spaces
            bb        #goto "b"
        }
        H             #appends line from the pattern space to the hold space, with a newline before it.
        ba            #goto "a"
    }
    :b                #Label "b"
    p                 #print
' file 

EDIT

If You want use regex for identify the lines, you can modify first command

sed -n '/goal/{p;n;h;:a;n;/forall/{p;x;bb};H;ba};:b;p' file

sed to replace specific positions on line with file contents, Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80  filename: is the file where sed is going to act. That will just put the result in the screen but the file will remain the same, you can redirect the output to a new file: sed '3iline 3' input.txt > output.txt. Shell script add a line to multiple files.


$ cat r.awk
BEGIN {
    forall_re = "^forall" # examples of regexps
    goal_re   = "^goal"
}

function tag(l) { # tag a line
    if      (l ~ goal_re  ) return "goal"
    else if (l ~ forall_re) return "forall"
    else                    return "rest"
}

{ # store entire file in array; give a tag to every line
    lines[NR] = $0 
    tags[NR]  = tag($0)
}

function swap0(a, i, j,   tmp) {
    tmp = a[i]; a[i] = a[j]; a[j] = tmp
}

function swap(i, j) {
    swap0(lines, i, j); swap0(tags, i, j)
}

function rise(i) {
    # TODO: add error check
    while (i - 1 > 0 && tags[i - 1] != "goal") {
        swap(i, i - 1); i--
    }
}

function process(    i) {
    for (i = 1; i <= NR; i++)
        if (tags[i] == "forall") rise(i)
}

function dump(    i) { # print the array
    for (i = 1; i <= NR; i++)
        print lines[i]
}

END {
    process()
    dump()
}

An example of input file

$ cat r.txt
goal identifier statement  
let statement 1  
let statement 2  
forall statement A  
other statements

goal identifier statement  
let statement 1  
let statement 2  
forall statement B
other statements

Usage:

$ awk -f r.awk r.txt
goal identifier statement  
forall statement A  
let statement 1  
let statement 2  
other statements

goal identifier statement  
forall statement B
let statement 1  
let statement 2  
other statements

Replace string in specific line at specific position of fixed-length file , Using sed: $ sed -E '/^SHIPTO/ s/(.{9}).{8}/\1NuValue /' file HEADER 123456 SHIPTO NuValue 123 LANE HOUSTON TX ITEM ACME BRICK. Substitute character by position - SED This might be useful in situations where you are interested to replace a range of positions in a file with some value/string. Your input will be - line number of the file, character range position, and "replace with" string/value.


sed is for simple substitutions on individual lines, that is all. For anything else you should use awk for every desirable attribute of software (clarity, simplicity, portability, etc.:

$ awk 'NR==FNR{if (/forall/) {f=FNR; v=$0} next} FNR!=f; /goal/{print v} ' file file
goal identifier statement
forall statement
let statement 1
let statement 2
other statements

50 `sed` Command Examples – Linux Hint, Replace a matched string with a string that contains newlines; Remove newlines from file and insert a comma at end of each line; Remove commas and add  I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case.


sed -r '/goal/{                 # if match "goal" line
    :X                          # this is a lable for branch command
    N                           # append next line
    /forall[^\n]*$/{            # if match "forall" line move to "goal" line below
        s#^([^\n]*)(.*)(\n[^\n]*)$#\1\3\2# 
        b                       # after move finished branch to end
    } 
    bX                          # branch to :X for appending next line
}' file
goal identifier statement  
forall statement  
let statement 1  
let statement 2  
other statements

The Basics of Using the Sed Stream Editor to Manipulate Text in , The sed stream editor is a non-interactive line editor that can perform powerful You will almost certainly never replace your regular text editor with sed , but it will sed operates on a stream of text that it reads from either a text file or from  sed "a" command lets us append lines to a file, based on the line number or regex provided. So, the lines will be added to the file AFTER the line where condition matches. sed "i" command lets us insert lines in a file, based on the line number or regex provided.


A less terrible way using vim to find a line in $FILENAME using regex $REGEX_EXPRESSION and move that line to $LINE_NUMBER:

vim -c "g:$REGEX_EXPRESSION:m$LINE_NUMBER" -cwq "$FILENAME"

explanation: -c is command in vim, so it goes to the first line that matches that regex and then moves it to the line number specified, and then does the command wq (or write and quit).

Manipulate Text from the Command Line with sed, It's possible to use sed to modify streams of text in file names in the current directory,) the move  1)In the first line i want to change from 18 position - 25 position to some other value- alpha numeric 2)if the line startd with 5900 then i should change 5 - 17 to some text Simiar to other lines also with different positions Need help on this


31+ Examples for sed Linux Command in Text Manipulation, Using multiple sed Linux commands in the command line for bash shell and replace it with csh shell in the /etc/passwd file using sed, And the appending works the same way, but look at the position of the appended text: You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed: sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo


Text Processing Commands, The useful -c option prefixes each line of the input file with its number of occurrences. 2) Modify the script to also filter out multiple spaces and #+ other whitespace. sed: # -n option, in combination with "p" flag to the "s" command, # outputs only matched lines. echo "Letters and periods can be in any position. We use sed to work with text files like log files, configuration files, and other text files. In this post, we are going to focus on the sed Linux command, which is used for text manipulation, which is an essential step in our bash scripting journey.


Add a line to a specific position in a file using Linux sed, Adding line to files in Linux, to an specific line position, using a shell script. Add a line to a specific position in a file using Linux sed You can then copy from /​tmp/ to your actual folder and you have been changed all files in  sed -i 's/word1/word2/g' input.file sed -i -e 's/word1/word2/g' -e 's/xx/yy/g' input.file ## use + separator instead of / ## sed -i 's+regex+new-text+g' file.txt The above replace all occurrences of characters in word1 in the pattern space with the corresponding characters from word2. Examples that use sed to find and replace . Let us create a