How to overwrite a line at a fixed position if a string in found in shell?

sed replace character at position
sed command
bash string replace
bash substring from end
bash replace string in file
awk replace character in string at position
bash string replace regex
replace string in shell script

I have an input file and if any line in that file has a particular key word i want to overwrite the data in a particular column position (say I want to populate column 10 to 15 with xxxxxx). I am new to shell scripting. Please forgive if I sound naive.

Sample Input:

aaaaa 11 ****** bacxyz more data
bbbbb 11 ****** qweabc more data
ccccc 11 ****** pqrxyz more data
aaaaa 11 ****** jkkxyz more data

Expected Output: (If a line has aaaaaa at any position overwrite the col 10 to 15 with xxxxxx else write as it is.)

aaaaa 11 ****** xxxxxx more data
bbbbb 11 ****** qweabc more data
ccccc 11 ****** pqrxyz more data
aaaaa 11 ****** xxxxxx more data

This sed may work for you:

sed -E '/aaaaa/ s/^(.{16}).{6}(.*)/\1xxxxxx\2/' file

aaaaa 11 ****** xxxxxx more data
bbbbb 11 ****** qweabc more data
ccccc 11 ****** pqrxyz more data
aaaaa 11 ****** xxxxxx more data

btw it is not position 10-15 but 17-22 in your expected output.

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. If you need to add a line to a file in Linux, and you need to add that line in a specific position of the file there is an easy solution, even if you need to do it to hundreds of thousands of files. As you can see we missed line 3, so to add it just execute this command: sed: is the command itself. 3: is the line where you want the new line

You could use the following Vim command:

:g/aaaaa/s/\%>16c\%<23c./x/g

This will replace characters between columns 16 and 23 with x. The replacement is performed in lines that contain the text aaaaa.

Search for a string at a particular position and replace with blank , Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to Am using korn shell. Hi, I am unable to find the right option to extract the data in the fixed width file. So, naturally I’m a huge fan of Bash command line and shell scripting. 15 years back, when I was working on different flavors of *nix, I used to write lot of code on C shell and Korn shell. Later years, when I started working on Linux as system administrator, I pretty much automated every possible task using Bash shell scripting.

Column data is usually easy with awk - "if the first field is aaaaa, make the fourth field xxxxx; then print the line, whatever it is":

awk '$1=="aaaaa"{$4="xxxxx"}{print}' filename

Replace nth position character of all the lines in file, I want to replace 150th character of all the lines in a file using sed or awk searched Shell Programming & Scripting To find nth position of character in string. I want to replace the 5th line of multiple text files (file1.txt,file2.txt,file3.txt,file4.txt) with the string " Good Morning " using a single terminal command. All the text files are located on my ~/Desktop. Note: My desktop consists of 6 .txt files.I want to apply the change to above mentioned 4 text files only.

Another global command

 :g/^a/norm 3wvt rx

Manipulating Strings, Bash supports a surprising number of string manipulation operations. 1) The script usually inserts a blank line at the end #+ of the target file. Fix this. Numerical position in $string of first character in $substring that matches. if [ -n "​$1" ] # If command-line argument present, then #+ then set start-string to it. str0="​$1" else  hi guys. i am a bit of an amateur in POSH, but trying my best :-) i have got this task where i need to match a string in a file, replace a word in the found pattern, and save the changes back to that file.

Manipulate Text from the Command Line with sed, If you find this guide helpful, please consider our guide to basic a character to separate the “find” and “replace” strings and to terminate the substitution script. It's possible to use sed to modify streams of text in shell scripts.

Bash String Manipulation Examples – Length, Substring, Find and , In bash shell, when you use a dollar sign followed by a variable name, shell Extract $length of characters substring from $string starting from $position. Find and Replace String Values inside Bash Shell Script 101 practical examples on both Bash command line and shell scripting. If It is fixed now.

magic of sed -- find and replace "text" in a string or a file · GitHub, should contain no more than one blank line between lines of text. sed '/^$/d;G' substitute (find and replace) "foo" with "bar" on each line. sed 's/foo/bar/' command may need an -e switch if you use Unix System V or bash shell. sed "s/.

Comments
  • Please add sample input and your desired output for that sample input to your question.
  • This sounds like the sort of thing that would be possible with sed (or perl if you prefer the easy way).
  • To be honest, I am just going through some documents on bash scripting, however I could not find any reference to how to do string manipulations specific to a particular column position.
  • I know it looks like I am begging to ask you the code (kinda do my work for free), but any pointer or any reference in this regard would be helpful. Thanks.
  • Read in bash documentation about "Parameter Expansion", in particular the bit about Substring Expansion. Note that you could also do this with sed or awk, and either of those tools would probably be better (and faster) at this than pure bash.
  • Yes. It worked. Thanks. Could you please explain this part '\1xxxxxx\2`? what's the significance of 1 and 2?
  • \1 and \2 are back-references of capturing groups #1 (.{16}) and #2 (.*).
  • "g" calls global command, "^a" letter "a" at beginning of the line. "norm" normal mode. "3w" jump three words "vt " visual select until next space. "rx" replace the selection with "x". Sorry, but my keyboard is not allowing me to use backticks in order to format the output correctly.