Sed replaces with carriage returns.
My file:
<ABC>123</ABC><ABC>456</ABC>
I want to output like this:
<ABC>123</ABC>
<ABC>456</ABC>
I am using:
sed "s/<ABC>///n/g"
But there isn't any changes.
Try as below
sed -e 's/></>\n</g' file.txt > output.txt
Related
I have a big ttl file and there are some errors when I load it
There are some special characters in the text that generates error :
<https://permid.org/1-5037622197>
a tr-org:Organization ;
mdaas:HeadquartersAddress "Germany\n"^^xsd:string ;
tr-common:hasPermId "5037622197"^^xsd:string;
tr-org:hasActivityStatus tr-org:statusActive ;
fibo-be-le-cb:isDomiciledIn <http://sws.geonames.org/2921044/> ;
vcard:organization-name "ARWOBAU Immobilien und Beteiligungs GmbH"^^xsd:string .
I want to get ride off all ^^xsd:string. I used sed command but this seem to not work, please find the code below
sed -i -e 's/^^xsd:string//g' test.txt
test.txt is the example above
My goal is to add a single apostrophe to every line in the file and skip empty lines.
file.txt:
Quote1
Quote2
Quote3
So far I have used sed:
sed -e "s/\(.*\)/'\1'/"
Which does the job but creates apostrophes also in empty lines:
'Quote1'
'Quote2'
''
'Quote3'
My goal:
'Quote1'
'Quote2'
'Quote3'
How could I achieve this by using sed, or maybe it should it be awk.
.* means zero or more characters, you want 1 or more characters which in any sed would be ..*:
$ sed "s/..*/'&'/" file
'Quote1'
'Quote2'
'Quote3'
You can also write that regexp as .\+ in GNU sed, .\{1,\} in POSIX seds, and .+ in GNU or OSX/BSD sed when invoked with -E.
The above assumes lines of all blanks should be quoted. If that's wrong then:
$ sed "s/.*[^[:blank:]].*/'&'/" file
'Quote1'
'Quote2'
'Quote3'
In any awk assuming lines of all blanks should be quoted:
$ awk '/./{$0="\047" $0 "\047"}1' file
'Quote1'
'Quote2'
'Quote3'
otherwise:
$ awk 'NF{$0="\047" $0 "\047"}1' file
'Quote1'
'Quote2'
'Quote3'
You can see the difference between the above with this:
$ printf ' \n' | sed "s/..*/'&'/"
' '
$ printf ' \n' | sed "s/.*[^[:blank:]].*/'&'/"
$ printf ' \n' | awk '/./{$0="\047" $0 "\047"}1'
' '
$ printf ' \n' | awk 'NF{$0="\047" $0 "\047"}1'
$
One way:
awk '$1{$0 = q $0 q}1' q="'" file
Add quotes only if 1st column($1) has some value. 1 to print every line.
Assuming you want to add the single quotes to lines that contain nothing but whitespace:
sed -E "/./s/(.*)/'\1'/"
Another sed
sed '/^$/!{s/^/\x27/;s/$/\x27/}' file
The above script says
Look for an empty line - /^$/ - pattern.
For those lines that doesn't follow the above pattern(!), substitute start(^) and end($) with single quotes(\x27).
You can use perl to check for a negative lookbehind, asserting that you won't match an "empty" line:
perl -pe 's/(?<!$)(.*)/"\1"/' file
Another alternative is being more specific in your regex, as #edmorton suggested in his answer.
tried on gnu sed
sed -E "s/\S+/'&'/" file.txt
I have this data in file.txt:
1234-abca-dgdsf-kds-2;abc dfsfds 2
123-abcdegfs-sdsd;dsfdsf dfd f
12523-cvjbsvndv-dvd-dvdv;dsfdsfpage
I want to replace the string after "-" and up to ";" with just ";", so that I get:
1234;abc dfsfds 2
123;dsfdsf dfd f
12523;dsfdsfpage
I tried with the command:
sed -e "s/-.*;/;" file.txt
But it gives me the following error:
sed command garbled
Why is this happening?
sed replacement commands are defined as (source):
's/REGEXP/REPLACEMENT/[FLAGS]'
(substitute) Match the regular-expression against the content of the pattern space. If found, replace matched string with REPLACEMENT.
However, you are saying:
sed "s/-.*;/;"
That is:
sed "s/REGEXP/REPLACEMENT"
And hence missing a "/" at the end of the expression. Just add it to have:
sed "s/-.*;/;/"
# ^
You are missing a slash at the end of the sed command:
Should be "s/-.*;/;/"
-.* here the * greedy, so this would fail if there are more than one ;
echo "12523-cvjbsvndv-dvd-dvdv;dsfdsfpage;test" | sed -e "s/-.*;/;/"
12523;test
Change to -[^;]*
echo "12523-cvjbsvndv-dvd-dvdv;dsfdsfpage;test" | sed -e "s/-[^;]*;/;/"
12523;dsfdsfpage;test
This should work :
sed 's/-.*;/;/g' file > newFile
I have a string in a file like this:
"Value1=[random number]"
After this string I want to add another string, specifically:
"Value2=100"
If I try to use:
sed '/Value1=/a Value2=100' myfile.txt
It will fail because I have not included the fact that Value1=[some random number].
How do I add the condition that Value1=random number and Value2 should be added to this string?
sed '/Value1=[0-9]\+/a Value2=100' myfile.txt
The [0-9]\+ will match any string of digits. For example, on my cygwin, GNU sed 4.2.2,
echo Value1=42 | sed '/Value1=[0-9]\+/a Value2=43'
produces
Value1=42
Value2=43
Edit: If the number may or may not be in double-quotes, use:
sed '/Value1="\?[0-9]\+"\?/a Value2=43'
The "\? is an optional double-quote.
Something like this:
$ sed 's,\(Value1=200\),\1 Value2=100,' myfile.txt
Result:
Value1=200 Value2=100
echo '"Value1=400"' | sed 's/"Value1=.*"/&\n"Value2=100"/'
Output:
"Value1=400"
"Value2=100"
Or:
echo '"Value1=400"' | sed 's/"Value1=.*"/& "Value2=100"/'
Output:
"Value1=400" "Value2=100"
I have one requirement that i have to read the file and manipulate. I have to replace the single double quote into double double quote if it is found in any fields. fields are separated by |.
Please find below for better understanding.
Input:
1234567|9393874|"Hi"|"How are "you""
98647489|20370483|"i am "good""|"what about "you""
output :
1234567|9393874|"Hi"|"How are ""you"""
98647489|20370483|"i am ""good"""|"what about ""you"""
I would replace all the "edge" quotes with another character and then replace the "inner" ones:
sed -e 's/|"/|_/g' -e 's/"|/_|/g' -e 's/"$/_/' file | sed 's/"/""/g' | sed 's/_/"/g'
It returns:
1234567|9393874|"Hi"|"How are ""you"""
98647489|20370483|"i am ""good"""|"what about ""you"""
Step by step:
$ sed -e 's/|"/|_/g' -e 's/"|/_|/g' -e 's/"$/_/' a
1234567|9393874|_Hi_|_How are "you"_
98647489|20370483|_i am "good"_|_what about "you"_
$ sed -e 's/|"/|_/g' -e 's/"|/_|/g' -e 's/"$/_/' a | sed 's/"/""/g'
1234567|9393874|_Hi_|_How are ""you""_
98647489|20370483|_i am ""good""_|_what about ""you""_
$ sed -e 's/|"/|_/g' -e 's/"|/_|/g' -e 's/"$/_/' a | sed 's/"/""/g' | sed 's/_/"/g'
1234567|9393874|"Hi"|"How are ""you"""
98647489|20370483|"i am ""good"""|"what about ""you"""