I have a list of ids as follows, and I want to convert them to a space separated ids (see output below)
INPUT:-
485238
478892
475507
467737
486413
483571
490005
OUTPUT:-
485238 478892 475507 467737 486413 483571 490005
This might work for you (GNU sed):
sed ':a;$!N;s/\n/ /;ta' file
But really this is a job for paste:
paste -sd\ file
N.B. there is a space after the backslash and a space before the file
Using awk, you can do this:
awk '{printf "%s ",$0}' file
485238 478892 475507 467737 486413 483571 490005
$ more test.txt
485238
478892
475507
467737
486413
483571
490005
$ sed ':a;N;$!ba;s/\n/ /g' test.txt
485238 478892 475507 467737 486413 483571 490005
If you want to send this to a new file:
$ sed ':a;N;$!ba;s/\n/ /g' test.txt > test2.txt
$ more test2.txt
485238 478892 475507 467737 486413 483571 490005
Related
How can I do these in sed?
#input #output
file.txt "nothing"
dir1/ ../
dir1/file.txt ../
dir1/dir2/ ../../
dir1/dir2/file.txt ../../
Let's say #input is placed to $var1
sed "do something" <<< $var1
echo $var1
You can try this GNU sed
sed "s#dir[0-9]\+/*#\.\./#g; s#file\.txt##g"
Is your test for dir1/dir2 (without trailing slash) correct? How does sed know if dir2 is a file or a directory? Otherwise you could use:
echo "dir1/dir2/file.txt" | sed s#[^/]*/#../#g | sed 's#[^/]*$##'
I have a example cut down from a log file.
112 172.172.172.1#50912 (ssl.bing.com):
I would like some how to remove the # and numbers after and (): from the url.
Would like the result.
112 172.172.172.1 ssl.bing.com
Here is the sed oneliner I have been working on.
cat newdns.log | sed -e 's/.*query: //' | cut -f 1 -d' ' | sort | uniq -c | sort -k2 > old.log
Thanks
Using sed, you could say:
sed 's/#[0-9]*//;s/(\(.*\)):$/\1/' filename
or, in a single substitution:
sed 's/#[0-9]* *(\(.*\)):$/ \1/' filename
Another sed:
sed -r 's/#[^ ]+|[():]//g'
$ echo '112 172.172.172.1#50912 (ssl.bing.com):' | sed -r 's/#[^ ]+|[():]//g'
112 172.172.172.1 ssl.bing.com
I want to replace path in
(setq myFile "/some/path")
in a file. I tried to do it with sed:
find ./_build/html -type f -name '*.html' | while read myFile; do
MyFile=`readlink -f "$myFile"`
sed -i "s/setq myFile [)]*/setq myFile \"$MyFile\"/" sphinx_nowrap.el
# and then some actions on file
done
and with perl:
find ./_build/html -type f -name '*.html' | while read myFile; do
MyFile=`readlink -f "$myFile"`
perl -ne "s/setq myFile .+/setq myFile \"$MyFile\")/" sphinx_nowrap.el
# and then some actions on file
done
but both give errors.
I've read this and this and also this -- but can't make it work.
Edit:
Here's a perl error:
Having no space between pattern and following word is deprecated at -e line 1.
Bareword found where operator expected at -e line 1, near "s/setq myFile .+/setq myFile "/home"
String found where operator expected at -e line 1, at end of line
(Missing semicolon on previous line?)
syntax error at -e line 1, near "s/setq myFile .+/setq myFile "/home"
Can't find string terminator '"' anywhere before EOF at -e line 1.
and here's sed error:
sed: -e expression #1, char 34: unknown option to `s'
Edit 2:
So the solution is to change the delimeter char. And also sed expression should be changed:
sed -i "s!setq myFile .*!setq myFile \"$MyFile\")!" sphinx_nowrap.el
Looks like perl (and sed) recognizes the slash in the file path as the regex delimiter. You can use a different delimiter:
find ./_build/html -type f -name '*.html' | while read myFile; do
MyFile=`readlink -f "$myFile"`
perl -ne "s!setq myFile .+!setq myFile \"$MyFile\")!" sphinx_nowrap.el
# and then some actions on file
done
or for sed:
find ./_build/html -type f -name '*.html' | while read myFile; do
MyFile=`readlink -f "$myFile"`
sed -i "s!setq myFile [)]*!setq myFile \"$MyFile\"!" sphinx_nowrap.el
# and then some actions on file
done
Lets assume your $MyPath hold /foo/bar/baz. Then the Perl code reads as:
perl -ne "s/setq myFile .+/setq myFile \"/foo/bar/baz\")/" sphinx_nowrap.el
Your Regex is terminated with the third / character. To work around this, we can use another delimiter like s{}{}:
perl -ine "s{setq myFile .+}{setq myFile \"/foo/bar/baz\")}; print" sphinx_nowrap.el
I also added the -i Option (inplace editing) and a print statement so that something actually gets print out.
But probably it would be more elegant to pass the value aof $MyPath as a command line argument:
perl -ne 's{setq myFile .+}{setq myFile "$ARGV[0]")}; print' $MyPath <sphinx_nowrap.el >sphinx_nowrap.el
New to sed and could use some help.
I would like to turn this "a/b/c a/b/c" into this "a/b/c a-b-c".
where a/b/c is any path.
thanks
Give this a try:
sed 'h; s/ .*//; x; s/.* //; s:/:-:g; x; G; s/\n/ /'
Since you want to use whitespace to delemit, I'd just use perl:
perl -ane '$F[1] =~ s/\//-/; print "#F\n"'
you can use awk,
$ echo "a/b/c a/b/c" | awk '{gsub("/","-",$NF)}1'
a/b/c a-b-c
This might work:
echo "a/b/c a/b/c" | sed ':a;s|\(.* [^/]*\)/|\1-|;ta'
a/b/c a-b-c
Or this:
echo "a/b/c a/b/c" | sed 's/.* //;h;y/\//-/;x;G;y/\n/ /'
a/b/c a-b-c
I have a file results.txt which is like:
a.txt
{some data}
success!!
b.txt
{some data}
success!!
c.txt
{some data}
error!!
I want to extract data from it. I want an output like:
a.txt: success
b.txt: success
c.txt: error
The problem is that the {some data} part can be arbitrarily long.
How can this be done?
awk:
BEGIN {
state=0
}
state==0 && /.txt$/ {
filename=$0
state=1
next
}
state==1 && /!!$/ {
print filename ": " gensub(/!!$/, "", $0)
state=0
next
}
$ cat file
a.txt
{some
blah
data}
success!!
b.txt
{some data}
success!!
c.txt
{some data}
error!!
$ awk 'BEGIN{ FS="[{}]|\n";RS=""}{gsub(/!!/,"",$NF);print $1":"$NF}' file
a.txt:success
b.txt:success
c.txt:error
Update:
$ awk -vRS= -vFS="\n" '{print $1":"$NF}' file
a.txt:success!!
b.txt:success!!
c.txt:error!!
You can use the following way also.
sed -e 's/^{some data}$//g;/^$/d;' results.txt | sed '$!N;s/\n/: /'
That works for me:
cat result.txt | xargs |sed 's/\ {[^}]*}/:/g' | sed 's/!! /\n/g'
a.txt: success
b.txt: success
c.txt: error!!
cat results.txt | grep -E "(([a-z]\.txt)|((success)|(error)!!))" | tr -d '\n' | sed 's/!!/!!\n/'
should do it. You might have to replace \n with a literal newline though.
awk '{print $1": "$4}' RS="\n\n" results.txt