I have a file with following strings:
1.44 1.12
disk 1.00 0.15 0.21
1.15 1.08
disk2 0.15 0.13 0.11
How to append 1 string to 2. Not 2 to 1. To get like this:
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
For example with sed?
With sed:
sed -n 'h;n;G;s/\n/ /;p' file
This will
h # save the line in the hold buffer
n # fetch the next line to the pattern space
G # append the hold buffer to the pattern space
s/\n/ / # replace the newline between them with a space
p # and print the result.
If you want to join two consecutive lines, you can for example say this in awk:
$ awk 'NR%2 {prev=$0; next} {print $0, prev}' file
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
This stores the odd lines in a variable prev and prints it later on together with the even line.
Here is another awk version if all lines needed starts with data:
awk '/^disk/ {print $0,p} {p=$0}' file
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
Some golfing:
awk '/^disk/&&$0=$0p; {p=$0}' file
disk 1.00 0.15 0.211.44 1.12
disk2 0.15 0.13 0.111.15 1.08
Related
I've a file with the following text:
<RecordID>02.037.D00221700080.0</RecordID>
2.35
AB
<RecordID>02.037.D00221700080.1</RecordID>
2.45
BB
<RecordID>02.037.D00221700080.2</RecordID>
6.5
CC
I wish to remove the dots, between <RecordID> and </RecordID> to get this:
<RecordID>02037D002217000800</RecordID>
2.35
AB
<RecordID>02037D002217000801</RecordID>
2.45
BB
<RecordID>02037D002217000802</RecordID>
6.5
CC
I've tried different approaches with sed, all of them without results...
Thanks in advance!
Using sed:
sed '/<RecordID>/s/\.//g' file
<RecordID>02037D002217000800</RecordID>
2.35
AB
<RecordID>02037D002217000801</RecordID>
2.45
BB
<RecordID>02037D002217000802</RecordID>
6.5
CC
Use this Perl one-liner:
perl -pe '/RecordID/ and tr/.//d;' in_file
The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlrequick: Perl regular expressions quick start
I found a problem, can anyone help to solve?
My Ubuntu "top" command cannot display Chinese characters correctly. It's using unrecognized characters, see the below:
joe#joe-us:~/app$ top -bc -n 1|grep home
1989 joe 20 0 34760 2564 2148 S 0 0.1 0:00.00 /usr/lib/gvfs//gvfs-fuse-daemon -f /home/joe/.gvfs
9577 joe 20 0 217m 40m 19m S 0 1.0 3:01.42 /home/joe/��?�?�/�?� sublime_text �??�?��?�
13885 joe 20 0 5828 820 732 S 0 0.0 0:00.00 grep --color=auto home
joe#joe-us:~/app$
For the same process, command "ps" works OK.
joe#joe-us:~/app$ ps -ef|grep home
joe 1989 1 0 10:11 ? 00:00:00 /usr/lib/gvfs//gvfs-fuse-daemon -f /home/joe/.gvfs
joe 9577 1 1 12:00 ? 00:03:01 /home/joe/桌面/到 sublime_text 的链接
joe 13883 12362 0 16:36 pts/0 00:00:00 grep --color=auto home
My system can support Chinese characters well now, here is the environment
joe#joe-us:~/app$ echo $LANGUAGE
zh_CN:zh
joe#joe-us:~/app$
joe#joe-us:~/桌面$ ls
aliedit.sh github github~ keeper.php Logo root root~ 到 sublime_text 的链接
joe#joe-us:~/桌面$
joe#joe-us:~$ echo $TERM
xterm
right display:/home/joe/桌面/到 sublime_text 的链接
wrong display:/home/joe/��?�?�/�?� sublime_text �??�?��?�
Thank you very much!
I have a matrix(5800 rows and 350 columns) of numbers. Each cell is either
0 / 0
1 / 1
2 / 2
What is the fastest way to remove all spaces in each cell, to have:
0/0
1/1
2/2
Sed, R, anything that will do it fastest.
If you are going for efficiency, you should probably use coreutils tr for such a simple task:
tr -d ' ' < infile
I compared the posted answers against a 300K file, using GNU awk, GNU sed, perl v5.14.2 and GNU coreutils v8.13. The tests were each run 30 times, this is the average:
awk - 1.52s user 0.01s system 99% cpu 1.529 total
sed - 0.89s user 0.00s system 99% cpu 0.900 total
perl - 0.59s user 0.00s system 98% cpu 0.600 total
tr - 0.02s user 0.00s system 90% cpu 0.020 total
All testes were run as above (cmd < infile) and with the output directed to /dev/null.
Using sed:
sed "s/ \/ /\//g" input.txt
It means:
Replace the string " / " (/ \/ /) by one slash (/\/) and do it globally (/g).
Here's an awk alternative that does exactly the same thing:
awk '{gsub(" ",""); print}' input.txt > output.txt
Explanations:
awk '{...}': invoke awk, then for each line do the stuff enclosed by braces.
gsub(" ","");: replace all space chars (single or multiple in a row) with the empty string.
print: print the entire line
input.txt: specifying your input file as argument to awk
> output.txt: redirect output to a file.
A perl solution could look like this:
perl -pwe 'tr/ //d' input.txt > output.txt
You can add the -i switch to do in-place edit.
I have o/p like
19599 user 20 0 120m 32m 4260 S 14.0 5.3 3:21.13 app.out \t Wed Jun 8 09:31:06 UTC 2011
19599 user 20 0 120m 32m 4260 S 14.0 5.4 3:21.61 app.out \t Wed Jun 8 09:31:12 UTC 2011
19599 user 20 0 121m 32m 4260 S 12.0 5.4 3:22.31 app.out \t Wed Jun 8 09:31:17 UTC 2011
I want to remove all character starting from \t in the line.
How can I do that with sed?
I tried with awk -F t '{print $1}'
but it removing t from app.out .
I want o/p like
19599 user 20 0 120m 32m 4260 S 14.0 5.3 3:21.13 app.out
19599 user 20 0 120m 32m 4260 S 14.0 5.4 3:21.61 app.out
19599 user 20 0 121m 32m 4260 S 12.0 5.4 3:22.31 app.out
If I wrote the awk like this:
awk -F t '{print $1"t"}'
it works fine, but it is only a work around. How can I remove all character starting from \t in the line till end of line?
If the output contains the two characters backslash and 't', then you use:
sed 's/ *\\t.*//'
This removes the blanks leading up to the two characters, the backslash and the 't', plus everything after them.
If the output contains a tab character, then you need to replace the '\\t' with an actual tab character.
It sounds like you want the first field in a tab-delimited text. You might try one of:
cut -d $'\t' -f 1
awk -F '\t' '{print $1}'
sed $'s/\t.*//'
The $'' syntax is used in bash (and ksh and zsh I believe) to more easily allow for embedding escape sequences in strings.
awk 'BEGIN { FS = "\t" } 1 == 1 {print $1}' file.name
Just pipe it through:
sed 's/\(.*\)\t.*/\1/'
I've been trying to break a sample file as below such that the third column becomes two parts while maintaining order within the file.
100 400 500.00APPLE 5.8 9.2
200 300 600.00DOG 5.3 9.1
300 763 454.44KITTEN 5.7 9.2
Should result in
100 400 500.00 APPLE 5.8 9.2
200 300 600.00 DOG 5.3 9.1
300 763 454.44 KITTEN 5.7 9.2
I've toyed doing this in awk but seem to be having issues.
PS: The point upon which to separate is always a digit [0-9] followed by [a-zA-Z] in regex.
Try:
sed 's/\([0-9]\)\([A-Z]\)/\1 \2/' ./infile
Proof of Concept
$ sed 's/\([0-9]\)\([A-Z]\)/\1 \2/' ./infile
100 400 500.00 APPLE 5.8 9.2
200 300 600.00 DOG 5.3 9.1
300 763 454.44 KITTEN 5.7 9.2
Or if you have gawk you can limit the split to just the 3rd field by using:
awk '{$3=gensub(/([0-9])([A-Z])/,"\\1 \\2","",$3)}1' ./infile
Proof of Concept
$ awk '{$3=gensub(/([0-9])([A-Z])/,"\\1 \\2","",$3)}1' ./infile
100 400 500.00 APPLE 5.8 9.2
200 300 600.00 DOG 5.3 9.1
300 763 454.44 KITTEN 5.7 9.2