I would like to edit a file on the command line.
I need to modify a set of 2 lines in this file.
Here are the 2 lines:
<parameter name="mail.smtp.from">synapse.demo.0#gmail.com</parameter>
</transportSender>-->
Here is the result I would like to have:
<parameter name="mail.smtp.from">synapse.demo.0#gmail.com</parameter>
</transportSender>
You can use command line editors like vi. It comes with almost all Unix systems. You can edit the file entering into insert mode. You can refer the following link.
Basic vi commands
I suppose it's a sed related question more than a WSO2IOT question, isn't it?
You already removed comment tag start and I suppose the line of the comment close tag is not unique…
Is line number a constant?
you could get your line number:
sed -i "`grep -n -A1 synapse.demo.0#gmail.com file.xml|tail -n1 | cut --delimiter="-" -f1`s/-->//" file.xml
Perl
perl -lpe 's|</transportSender>-->|</transportSender>|' your-file Just Once
perl -lpe 's|</transportSender>-->|</transportSender>|g' your-file multiple
To save in-place
perl -i -lpe 's|</transportSender>-->|</transportSender>| your-file' find and edit and save all together
Related
I've an input file that consists of IP Address and subnet masks. As an example,
1.example.com,10.135.10.111,255.255.255.0,some comment
2.example.com,10.135.10.112,255.255.255.0,some comment
3.example.com,10.135.10.113,255.255.255.0,some comment
4.example.com,10.135.10.11,255.255.255.0, some comment
10.135.10.111 A
10.135.10.112 A
10.135.10.113 A
10.135.10.11 A
I loop the IP address in my bash script and when using the perl or sed command all .11 gets changed. As an example:
inputip=10.135.10.11
newip=10.135.10.77
perl -i -e 's/$inputip/$newip/g' inputfile
OR
sed -e "s/$inputip/$newip/g" inputfile
The problem is all instance of .11 gets changed. so the above record of 10.135.10.111 is changed to 10.135.10.771, .772, .773, .77
Note: this line 10.135.10.11 A is not necessarily the last line, it's anywhere in the file.
There are four problems with the Perl version.
You're missing -p.
You expected Perl to use the shell variables $inputip and $newip, but those are only found in the shell process. There's a number of ways to pass values to perl, as you can see in How can I process options using Perl in -n or -p mode?.
There's also a code injection bug, where . isn't matched literally.
There's an anchoring problem, where you accidentally modify IP addresses you don't want to change. For example, you will corrupt 210.135.10.11 when trying to change 10.135.10.11.
Fixed:
perl -i -spe's/\b\Q$o\E\b/$n/g' -- -o="$inputip" -n="$newip" inputfile
Input:
$ cat inputfile
1.example.com,10.135.10.111,255.255.255.0,some comment
2.example.com,10.135.10.112,255.255.255.0,some comment
3.example.com,10.135.10.113,255.255.255.0,some comment
4.example.com,10.135.10.11,255.255.255.0, some comment
4.example.com,210.135.10.11,255.255.255.0, some comment
10.135.10.111 A
10.135.10.112 A
10.135.10.113 A
10.135.10.11 A
210.135.10.11 A
With GNU sed and word boundary sequences (\< / \>):
$ inputip=10.135.10.11
$ newip=10.135.10.77
$ sed "s/\<$inputip\>/$newip/g" inputfile
1.example.com,10.135.10.111,255.255.255.0,some comment
2.example.com,10.135.10.112,255.255.255.0,some comment
3.example.com,10.135.10.113,255.255.255.0,some comment
4.example.com,10.135.10.77,255.255.255.0, some comment
4.example.com,210.135.10.11,255.255.255.0, some comment
10.135.10.111 A
10.135.10.112 A
10.135.10.113 A
10.135.10.77 A
210.135.10.11 A
If this does not work then OP is likely not using GNU sed; in this case we'd need to know what version of sed is in use (eg, sed --version).
I am trying to append a line under a specific line, lets say [BELOW HERE] and without using SED. What is the best alternative way to do this?
I've tried to use SED but this was not supported for the machine where the script was made for.
sed --in-place "/^\[BELOW HERE\]/a BLabla=Database toolSomething" file
/a = append
You don't need awk, sed or any other third party built-ins for this trivial task. You can use the ed editor available from the UNIX days available in all major distributions these days,
printf '%s\n' 'g/[BELOW HERE]/s/\r/BLabla=Database toolSomething
/g' w q | ed -s file
Using awk and its gsub function.
awk '/BELOW HERE/{gsub(/$/,"&\nTHIS IS NEW LINE ADDED HERE")}1' input
I am trying to append a line under a specific line,
lets say [BELOW HERE] and without using SED.
THIS IS NEW LINE ADDED HERE
What is the best alternative way to do this?
Not sure you do not wish to use sed : Here is one sed solution:
sh-4.1$ sed '/BELOW HERE/a\
"THIS IS NEW LINE ADDED HERE"' input
I am trying to append a line under a specific line,
lets say [BELOW HERE] and without using SED.
"THIS IS NEW LINE ADDED HERE"
What is the best alternative way to do this?
sh-4.1$
Input file:
cat input
I am trying to append a line under a specific line,
lets say [BELOW HERE] and without using SED.
What is the best alternative way to do this?
I have a text file with a unicode line separator (hex code 2028).
I want to remove it using bash (I see implementations for Python, but not for this language). What command could I use to transform the text file (output4.txt) to lose the unicode line separator?
See in vim below:
Probably this tr command should also work:
tr '\xE2\x80\xA8' ' ' < inFile > outFIle
Working solution: Thanks to OP for finding this:
sed -i.old $'s/\xE2\x80\xA8/ /g' inFile
I noticed that in your screenshot, you have already opened file in vim, then why not just do the substitution in vim?
in vim you could do
:%s/(seebelow)//g
the (seebelow) part, you could type:
ctrl-vu2028
You can probably use sed:
sed 's/\x20\x28//g' <file_in.txt >file_out.txt
To overwrite the original file:
sed -i 's/\x20\x28//g' file.txt
Edit: (See chepner's comment) You should make sure that you have the correct bytes, depending on the encoding, and then use sed to delete them. You could use e.g. od -t x1 for looking at the hex dump and figuring out the encoding.
This worked for me
sed $'s/\u2028//g' file_in.txt > file_out.txt
Note: other questions use the term <U+2028>
I have a .csv file where I'd like to delete the lines between line 355686 and line 1048576.
I used the following command in Terminal (on MacOSx):
sed -i.bak -e '355686,1048576d' trips3.csv
This produces a file called trips3.csv.bak -- but it still has a total of 1,048,576 lines when I reopen it in Excel.
Any thoughts or suggestions you have are welcome and appreciated!
I suspect the problem is that excel is using carriage return (\r, octal 015) to separate records, while sed assumes lines are separated by linefeed (\n, octal 012); this means that sed will treat the entire file as one really long line. I don't think there's an easy way to get sed to get sed to recognize CR as a line delimiter, but it's easy with perl:
perl -n -015 -i.bak -e 'print if $. < 355686 || $. > 1048576' trips3.csv
(Note: if 1048576 is the number of "lines" in the file, you can leave off the || $. > 1048576 part.)
Not sure about the osx sed implementation, however the gnu sed implementation when passed the -i flag with a backup extension first copies the original file to the specified backup and modifies the original file in-place. You should expect to see a reduced number of lines in the original file trip3.csv
Some incantation that should do the job (if you have Ruby installed, obviously)
ruby -pe 'exit if $. > 355686' < trips3.csv > output.csv
If you prefer Perl/Python, just follow the documentation to do something similar and you should be fine. :)
Also, I'm using one of the Ruby one-liners, by Dave.
EDIT: Sorry, forgot to say that you need '> output.csv' to redirect stdout to a file.
awk '!(NR>355686 && NR <1048576)' your_file
I'm looking for a way to remove lines within multiple csv files, in bash using sed, awk or anything appropriate where the file ends in 0.
So there are multiple csv files, their format is:
EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLElong,60,0
EXAMPLEcon,120,6
EXAMPLEdev,60,0
EXAMPLErandom,30,6
So the file will be amended to:
EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLEcon,120,6
EXAMPLErandom,30,6
A problem which I can see arising is distinguishing between double digits that end in zero and 0 itself.
So any ideas?
Using your file, something like this?
$ sed '/,0$/d' test.txt
EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLEcon,120,6
EXAMPLErandom,30,6
For this particular problem, sed is perfect, as the others have pointed out. However, awk is more flexible, i.e. you can filter on an arbitrary column:
awk -F, '$3!=0' test.csv
This will print the entire line is column 3 is not 0.
use sed to only remove lines ending with ",0":
sed '/,0$/d'
you can also use awk,
$ awk -F"," '$NF!=0' file
EXAMPLEfoo,60,6
EXAMPLEbar,30,10
EXAMPLEcon,120,6
EXAMPLErandom,30,6
this just says check the last field for 0 and don't print if its found.
sed '/,[ \t]*0$/d' file
I would tend to sed, but there is an egrep (or: grep -e) -solution too:
egrep -v ",0$" example.csv