Replacing the nth character, only if is a given symbol - sed

I am new on sed and I am trying to replace the nth character of a line, only if is a symbol "-"
for example, replace the 4th position for an "X" only if is "-":
aaabbbccc
aaa-bbddd
aaa-cccbb
to get:
aaabbbccc
aaaXbbddd
aaaXcccbb
I found that is possible to replace the nth position with this command:
sed 's/^(.{3}).(.*)/\1X\2/' but I am not able to incorporate the given condition (only if is a given symbol)
Thanks in advance!

echo "aaabbbccc
aaa-bbddd
aaa-cccbb"|sed -r 's/^(.{3})-(.*)$/\1X\2/'
output:
aaabbbccc
aaaXbbddd
aaaXcccbb

This might work for you:
sed 's/^\(.\{3\}\)-/\1X/' file
Or
sed '/^.\{3\}-/s/./X/4' file

Related

Delete line with specific number of characters

I have a specific file (file.txt) with several lines.
How is it possible to delete all lines that do not have 12 characters, using sed?
Use an interval expression to specify the exact number of characters you want to match between the beginning (^) and end ($) of the input record.
sed '/^.\{12\}$/!d' file
Not sure why you would use sed. This is much cleaner in awk:
awk 'length == 12' file.txt

How to replace the nth occurrence of a sttring in a file using sed [duplicate]

This question already has answers here:
How to replace the nth occurrence of a string using sed
(2 answers)
Closed 8 years ago.
Is there any way to replace the nth occurrence of a string in a file using sed?
How can i change it so that it replaces the nth occurrence?
My file contents the following lines:
first line
second line
third line
jack
fifth line
jack
seventh line
consider a variable var = jill.
I want to replace the 2nd occurrence of jack with value of variable $var.
This might work for you (GNU sed):
sed ':a;$!{N;ba};s/jack/'"$var"'/2' file
This slurps the file into memory and then substitutes the second occurence of jack for $var.
EDIT:
:a is a place holder for a b command, it tell sed to break/jump to position i.e ba jump to :a.
$! is an address. $ means end of file and ! means not. Put together this means any address which is not-the-end-of-file.
{...} groups the commands between the braces.
N appends a newline and then the next line to the pattern space except when there are no more lines when it passes sed to just passed the last command (if the -n is not set it will print what ever is in the pattern space, if -n is set it will just end processing).
s/jack/'$var"'/2 this is a sed substitution command and replaces the second occurrence of jack by the contents of $var. N.B. the '...' which effectively breaks out sed commands into the underlying shell and then back again allowing the shell variable to be interpolated.
In summary the whole file is slurped into memory and the second occurrence of jack is replaced by the contents of $var.
In most cases this could by replaced by:
sed -z 's/jack/'"$var"'/2' file
Here is an awk version if you like to use awk
awk -v c="$var" '/jack/ && ++a==2 {sub(/jack/,c)}1' file
first line
second line
third line
jack
fifth line
jill
seventh line

SED Command to remove first digits and spaces of each line

I have a simple text file in below format.
1 12658003Y
2 34345345N
3 34653785Y
4 36452342N
5 86747488Y
6 34634543Y
so on
10 37456338Y
11 33535555Y
12 37456378Y
so on
100 23432434Y
As you can see there are two white spaces after first number.
I'm trying to write SED command to remove the digits before whitespaces. Is there any SED command to remove spaces and number before spaces?
Output file should look like below.
12658003Y
34345345N
34653785Y
36452342N
so on..
Please assist. I'm very new to shell scripting.
sed 's/[0-9]\+\s\+//' infile > outfile
Explanation:
s: we want to use substitution
/: mark start and end of the expression we want to match
[0-9]: match any digit
+: match the previous one or more time
\s: space
+: match the previous one or more time
/: mark start of what we want to change our matches to (which is nothing)
/: some special operators goes after this (we use no such)
infile: the file we want to change
>: pipe stdout to
outfile: where we want to store output
Your sed command would be,
sed 's/.* //g' file
This would remove the first numbers along with the space followed.
Remove leading digits, then following spaces:
sed 's/^[0-9]* *//' file
sed 's/^[0-9]*[ ]*//g' input.txt

How to remove a specific letter within the word of each line of a file

I need to remove a character after a number in each line. How can I do the job by using sed?
I used sed command to remove but failed.
Input:
481065919|g UPF0114
481065919|g PRK12772
I need to remove |g from each line.
Expected output:
481065919 UPF0114
Removing is the same as replacing with nothing. This works for me:
sed -i "s/|5//g" test.txt
Can also be done with awk
awk '{gsub(/\|g/,"")}8' file
gsub replace anything found between /.../
To find |g you need to escape the | since its a special character, so \|g
Replace with "", means nothing (same as delete)
8 is just any positive number, do default action, print anything.
8 does the same as 1 and the same as 1 {print} and the same as 1 {print $0}

How to replace the nth occurrence of a string using sed

Is there any way to replace the nth occurrence of a string in a file using sed?
I'm using sed -i '0,/jack.*/ s//jill/' to replace the first occurrence.
How can i change it so that it replaces the nth occurrence?
My file contents the following lines:
first line
second line
third line
jack=1
fifth line
jack=
seventh line
I don't know the value after jack=, it can be anything or nothing.
I want to replace the 2nd occurrence of jack= and anything that follows it with jill.
First replace all the newlines with a unique character that does not occur anywhere else in your file (e.g. ^) using tr. You need to do this in order to create a single string for sed. Then pass it to sed and tell it to replace the nth occurrence of your string. Finally, pass the output back through tr to recreate the newlines.
For n=2, the command is:
$ tr '\n' '^' < file | sed 's/jack/jill/2' | tr '^' '\n'
first line
second line
third line
jack
fifth line
jill
seventh line
Update:
It can also be done with sed, WITHOUT changing the newlines first, using the following command:
$ sed ':a;N;$!ba;s/jack/jill/2' file
Alternatively, use awk:
$ awk '/jack/{c+=1}{if(c==2){sub("jack","jill",$0)};print}' file
Try this, sed ':a;N;$!ba;s/word1/word2/n' filename
Here, :a;N;$!ba is used to load the entire file into memory, line by line, so that sed can process the whole file in a single pass. The s/word1/word2/N substitution then replaces every Nth occurrence of word1 with word2.