Sed remove all content in line after \t - sed

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/'

Related

Debugging a specified emacs of multiple, when one is frozen

Two emacs server are running on my machine
$ ps -ef |grep emacs | sed "s/$USER/me/g"
me 4010 1 6 13:52 ? 00:02:58 /snap/emacs/25/usr/bin/emacs --daemon=orging
me 4538 1 3 13:52 ? 00:01:45 /snap/emacs/25/usr/bin/emacs --daemon=coding
me 4622 1 0 13:52 pts/1 00:00:00 /snap/emacs/25/usr/bin/emacsclient /home/me/ORG/os.org -c -s orging
me 4623 1 0 13:52 pts/1 00:00:00 /snap/emacs/25/usr/bin/emacsclient /home/me/ORG/algorithms.org -c -s coding
me 8945 3548 0 14:38 pts/1 00:00:00 grep --color=auto emacs
The one of 'orging` is frozen,
Upon reading debugging - What do I do when Emacs is frozen? - Emacs Stack Exchange, I got the solution
pkill -SIGUSR2 emacs
How could apply the operation to the specified emacs saying 'orging'?
Use the kill command and specify the pid of the emacs instance you want, in this case 4010:
kill -SIGUSR2 4010

Remove all strings before tab (include tab) in a line of text

A text file (file name: 1.txt):
Incoming_Queries_A: 13201096
Incoming_Queries_A6: 946
Incoming_Queries_AAAA: 1288191
Incoming_Queries_ANY: 31280
Incoming_Queries_AXFR: 5
Incoming_Queries_CNAME: 410
Incoming_Queries_DS: 20
Incoming_Queries_MX: 854
Incoming_Queries_NS: 97217
Incoming_Queries_PTR: 1011409
Incoming_Queries_SOA: 5006
Incoming_Queries_SPF: 1
Incoming_Queries_SRV: 3555
Incoming_Queries_TXT: 511
Incoming_Requests_IQUERY: 11
Incoming_Requests_NOTIFY: 1
Incoming_Requests_QUERY: 15640501
Incoming_Requests_STATUS: 1
Incoming_Requests_UPDATE: 5
I want to remove all strings before tab (include tab) in a line of text and print the output(example: 13201096 ) to standard out.
Example:
# egrep -i "Incoming_Queries_A:" ./1.txt | sed 's/.Incoming_Queries_A:\t//'
Output:
Incoming_Queries_A: 13201096
But I only want to output 13201096
How to fix it? thanks
sed doesn't automatically handle escaped chars (such as \t and \n) on bash. You can handle it in two different ways:
You can replace the \t by an actual tab in your expression. To hit a tab in the terminal, you do Control-V, then hit the TAB key: Ctrl-V and then Tab.
(this one seems far more elegant, IMO) you can force sed to interpret your \t, by placing a $ before your substitution string. This way, your command would be like:
egrep -i "Incoming_Queries_A:" ./1.txt | sed $'s/Incoming_Queries_A:\t//'
(I removed that . before Incoming_Queries_A: - probably a typo/desperate tentative)
Hope that helps.
Since you only need the second column, you can use cut:
cut -f2 file.txt
With awkyou can do this like that:
awk '{print $2}' 1.txt
13201096
946
1288191
31280
5
410
20
854
97217
1011409
5006
1
3555
511
11
1
15640501
1
5
Or awk '/Incoming_Queries_A:/ {print $2}' /tmp/t.txt for get only the line you wanted
You could try the below GNU sed command,
sed -r 's/^.*\t//' file

"top" command can not support chinese characters in ubuntu 12.04

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!

Extract every nth number from a txt file

So I have a txt file where I need to extract every third number and print it to separate file using Terminal. The txt file is just a long list of numbers, tab delimited:
18 25 0 18 24 5 18 23 5 18 22 8.2 ...
I know there is a way to do this using sed or awk, but so far I've only been able to extract every third line by using:
awk 'NR%3==1' testRain.txt > rainOnly.txt
So here's the answer (or rather, the answer I utilized!):
xargs -n1 < input.txt | awk '!(NR%3)' > output.txt
This gives you an output.txt that has every third number of the original file as a separate line.
A quick pipe line to extract every 3rd number:
$ xargs -n1 < file | sed '3~3!d'
0
5
5
8.2
If you don't want each number on a newline throw the result back through xargs:
$ xargs -n1 < file | sed '3~3!d' | xargs
0 5 5 8.2
Use redirection to store the output in a new file:
$ xargs -n1 < file | sed '3~3!d' | xargs > new_file
With awk using a simple for loop you could do:
$ awk '{for(i=3;i<=NF;i+=3)print $i}' file
0
5
5
8.2
or (adds a trailing tab):
$ awk '{for(i=3;i<=NF;i+=3)printf "%s\t",$i;print ""}' file
0 5 5 8.2
Or by setting the value of RS (adds trailing newline):
$ awk '!(NR%3)' RS='\t' file
0
5
5
8.2
$ awk '!(NR%3)' RS='\t' ORS='\t' file
0 5 5 8.2
You can print every third character by substituting the next two with nothing, globally. When the count straddles a newline, using Perl might be the simplest solution:
perl -p000 -e 's/(.)../$1/gs'
If you want the first, fourth etc character from every line, a line-oriented tool like sed suffices:
sed 's/\(.\)../\1/g'
Using grep -P
grep -oP '([^\t]+\t){2}\K[^\t\n]+' file
0
5
5
8.2
This might work for you (GNU sed):
sed -r 's/(\S+\s){3}/\1/g;s/\s$//' file
#user2718946
Your solution was close, but here you are without xarg.
awk 'NR%3==1' RS=" " file
18
18
18
18
Different start:
awk 'NR%3==0' RS=" " file
0
5
5
8.2

How to make a UK date out of a standard date format string using sed?

The string Fri May 3 13:50:37 2013 has to be reorder to Fri 3 May 2013 13:50:37. How can this be achieved using sed?
sed isn't the right tool here you want awk:
$ echo "Fri May 3 13:50:37 2013" | awk '{print $1,$3,$2,$5,$4}'
Fri 3 May 2013 13:50:37
In awk $i represents the ith field so just arrange the fields in the order required.
If date command could work for a date handling, I would stand with date command.
kent$ date -d"Fri May 3 13:50:37 2013" +"%a%e %b %Y %T" │ %M minute (00..59)
Fri 3 May 2013 13:50:37
Use the back references in the regular expression and match the subpatterns.
sed -E 's/^(...) (...) ([0-9]+)(.*)([0-9]{4})$/\1 \3 \2 \5 \4/'
This might work for you (GNU sed):
sed -r 's/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([123]?[0-9]) /\2 \1 /g' file