New answers tagged

0

So far I've already solved the merging problem sudo nethogs wlan0 | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" Now I need to merge a time stamp


0

To monitor wlan0 sudo nethogs wlan0 You can monitor network bandwidth of both eth0 and eth1 interfaces nethogs [option] eth0 eth1 options : -d delay for refresh rate. For example, to set 5 seconds as your refresh rate, then type the command as. sudo nethogs -d 5 -h display available commands usage. -p sniff in promiscious mode (not ...


0

awk '{arr[$1]++} END {for (i in arr) {if (arr[i]==1) {print i} }}' 1 grapes lime peach


0

Or, to show all different line: sort test.data | uniq


4

That's the job for uniq: $ LC_ALL=C uniq -u file grapes lime peach If you want other tools, like perl: perl -nle '$h{$_}++; END {print for grep { $h{$_} == 1 } %h}' <file


0

Try this AWK! awk '{a[$0]++} END {for (x in a) if (a[x] == 1) print x}'


-1

Try the following: awk '{print $2 $1}' filename.txt >> newfilename.txt


1

If you have access to GNU grep: grep -vP '^(\d+-\d+-\d+|=+)$' file And, if you don't: grep -vE '^([0-9]+-[0-9]+-[0-9]+|=+)$' file Both commands use grep's -v flag which means "print lines that don't match the pattern and look for lines consisting of either 3 groups of digits separated by dashes or one or more = from the beginning (^) to the end ($) of ...


0

There are many ways to do this. Here is another. You can figure out more ways. echo "unix is great os." | tr [a-z] [A-Z] UNIX IS GREAT OS.


0

An alternative solution in AWK: awk -v FS="," '/^[0-9]/{line=$0;getline; line=line" "$1", "$2 ;print line}' file


2

You can use sed: sed -ne '/^[0-9][0-9]*\.[0-6]/ { N; s/\n/ /; s/^\([^,]*,[^,]*\),.*$/\1/; p; }' < data This processes a file called data, suppressing printing unless asked for (-n) and executing the sed program in quotes. That program selects lines starting with one or more digits, a ., and a digit 0-6, and then runs the part in {} for those lines. ...


0

This script works for me. I tested it in GNU Awk 4.0.1, but should work in Nawk as well. awk 'BEGIN { # action=0: uncomment # action=1: comment action=0 in_optional_code_block=0 } { if ($0 ~ /^# BEGIN/) { in_optional_code_block=1 } else if ($0 ~ /^# END/) { in_optional_code_block=0 } else if ...


4

Try grep: grep -iv dog inputfile -i to ignore case and -v to invert the matches. If you want to use sed you can do: sed '/[dD][oO][gG]/d' inputfile In sed, there is also the I flag, which should make the match case insensitive, but as far as I remember this does not work in all flavors of sed. For me, this works: sed '/dog/Id' inputfile but it ...


2

Your sed command only sends its result to the standard output. You would have to redirect it in a subsequent command (NOT in the same command, like sed 'sedcommand' file > file, as this would erase the file before processing it). You also can pipe the commands to ed instead of using sed : for file in $filelist ; do echo -e ...


6

Try this: sed 's/yyyymmdd/YYYYMMDDHH24MISS/g' filename > changed.txt Or, to keep the same filename: sed 's/yyyymmdd/YYYYMMDDHH24MISS/g' filename > changed.txt && mv changed.txt filename


0

You could use grep and cut: ~$ mount | egrep -o 'addr=[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d '=' -f 2 12.156.0.212 112.166.60.12 123.10.10.12


2

Start from the rightmost one: sed -i 's/./&,/22;s/./&,/8;s/./&,/4' ./*.txt Otherwise, the first substitution would affect the offset for the second. You can always account for it though: sed -i 's/./&,/4;s/./&,/9;s/./&,/24' ./*.txt


2

I created a file called json that looks like this: [{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"},{"product":"Pear","id":"1111"},{"product":"Banana","id":"2222"}] Then I run this on the command line: cat json | sed -e 's/.\?{"product"\:\"\([^\"]\+\)\","id"\:\"\([[:digit:]]\+\)[^}]}\+.\?/Enter Product : \1\nYour Product id is : ...


1

sed is clumsy, but here is what I could get - sed 's/.*\(Apple\)[^:]*:"\([0-9]*\)".*/Enter product ID: \1\nYour product ID is: \2/' 3 Enter product ID: Apple Your product ID is: 2134 sed 's/.*\(Mango\)[^:]*:"\([0-9]*\)".*/Enter product ID: \1\nYour product ID is: \2/' 3 Enter product ID: Mango Your product ID is: 4567 Edited based on latest input ...


2

Use a json parser, not sed/grep/awk. Using Python json module: #!/usr/bin/env python2 import json with open('file.json') as f: f_json = json.load(f) print 'Enter product : ' + f_json[0]['product'] + '\nYour product id is : ' + f_json[0]['id'] Output: Enter product : Apple Your product id is : 2134


6

Use a JSON aware tool. Perl has the JSON library: #!/usr/bin/perl use warnings; use strict; use JSON; my $json = '[{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"}]'; print 'Enter product: '; chomp( my $product = <> ); print 'Your product id is: ', (grep $_->{product} eq 'Apple', @{ from_json($json) })[0]{id}, "\n";


1

Try this awk script: /CREATE/{ b=""; while (match($0, ";$")==0) { b=b $0"\n"; getline; } b=b $0; print b > "file1"; } /REPLACE/{ c=""; while (match($0, ";$")==0) { c=c $0"\n"; getline; } c=c $0; print c > "file2"; } So awk -f some.awk inputFile produces: cat file1 CREATE ...


1

It seems you want the lines not having /0/ on the last column, you can do: grep -v '[^[:blank:]]*/0/[^[:blank:]]*$' file.txt Example: % grep -v '[^[:blank:]]*/0/[^[:blank:]]*$' file Row2: column1, column2, column3...column10, ht*p://narph0.net/page/328/narph.htm Row4: column1, column2, column3...column10, ht*p://www.the.com/thethat/que303/yeah/main.php ...


1

Sticking with your awk ... just make sure you understand the difference between a field and a record separator :} echo "a,b,c,d,e,f" | awk 'BEGIN{RS=","}{$1=$1}1' But the tr solution in the comments is preferable.


1

Python Solution: #!/usr/bin/python3 #cycle.py old_server = 'avocado' new_servers = ['beet','banana','cherry','tomato'] replacement_count = 0 with open('example.txt') as file: #cycle through file for line in file: if old_server in line: replacement_count += 1 #increment counter to cycle through new servers #print ...


2

$ gawk '{print > "file" ++a[$1] ".txt"}' input # And on OSX awk, and also gawk: $ awk '{print > ("file" ++a[$1] ".txt")}' input $ head file*txt ==> file1.txt <== 1 Cat 2 Boy 3 Tree ==> file2.txt <== 1 Dog 2 Girl 3 Leaf ==> file3.txt <== 1 Frog 3 Branch ==> file4.txt <== 3 Trunk edit: An ...


2

Here's one way with sed: sed -n 'H # append every line to hold buffer /CREATE\|REPLACE/h # if CREATE or REPLACE, overwrite hold buffer /;/{ # if line matches ; then x # exchange hold space w. pattern space /CREATE/w file1 # if pattern space matches CREATE, ...


4

Try awk awk ' NR==FNR{ A[NR]=$1 limit=NR next } /^avocado/{ i=i%limit+1 $1=A[i] } { print } ' newservers.lst servers.txt Sed is possible too: sed '/^\s*\S\+\s*$/ { #match 1st file only x #exchange line with holdspace H ...


0

You can do the following: find -name "data.csv" | xargs -n 1 sed -n 2p >> final.csv Note above that using >> concatenates results from sed into final.csv, on a new line, instead of replacing them, whereas > simply replaces content in final.csv with output from sed.


0

The result of the find is the list of filenames that sed needs to act upon. Thus use xargs to execute sed on that list, one by one. But because filenames can contains spaces and newlines even, better use the "-print0" option of find, to delimit the filenames with ascii zero. Also, when those files are big, you can save cpu-heat by stopping after line 2. ...


1

If you look at the output of find -name "data.csv", you'll see that it prints the full pathnames of files named data.csv. The pipeline passes that on to sed, which prints the second line of its input. So what you're saying is, "Here's a list of files. Give me the second one one the list." What you really want to say is "Here's a list of files. For each one, ...


1

Pythonic way: >>> with open("/home/xieerqi/textfile.txt") as file: ... for line in file: ... if line.__contains__("foo"): ... VAR = line ... if line.__contains__("relevant=yes"): ... print VAR ... foo.bar.1 foo.bar.4 Put together in a script: DIR:/xieerqi skolodya@ubuntu:$ chmod +x relevance.py ...


0

Just want to add little more to @chaos solution hexdump -ve '1/1 "%.2X "' input | sed 's/.*\(FF D8.*FF D9\).*/\1/' | xxd -r -p > image.jpeg I have just added space after %.2X and, between FFD8 and FFD9. This is to avoid matching the shifted pattern such as: 0f fd 80 ... 0f fd 90


3

Here's one way with sed: sed '/foo\.bar/h;/relevant=yes/!d;x;/foo\.bar/!d' infile Lines matching foobar are copied to hold space. All lines, except those matching relevant=yes are deleted. Exchange hold space with pattern space (this only happens when lines match relevant=yes) and delete if it doesn't match foobar.


5

If the input is processed line by line, then processing needs to go like this: if the current line is foo.bar, store it, forgetting any previous foo.bar line that wasn't enabled for output; if the current line is relevant=yes, this enables the latest foo.bar for output. This kind of reasoning is a job for awk. (It can also be done in sed if you like ...


0

This answer borrows ideas from this other answer and this other answer but builds on them, creating an answer that is more generally applicable: num_bytes=$(sed '/myregex/d' /path/to/file/filename | wc -c) sed '/myregex/d' /path/to/file/filename 1<> /path/to/file/filename dd if=/dev/null of=/path/to/file/filename bs="$num_bytes" seek=1 The first line ...


2

One possible way to do it would be to use perl's unpack function, with a template constructed from the first line of the file. After removing whitespace, it uses a regular expression with a repeating backreference to find the longest contiguous strings of identical characters not exceeding the given maxwidth, and stores their positions in an array. The ...


1

You asked for nawk or sed. As you mention ksh, why not do a pure shell version? The following runs in any POSIX compliant shell: p="Apple" while read line ; do case $line in (*$p*) echo $p${line#*$p};; esac done <<! john_Apple01_xyz_1 john_Fruit_Apple01_abc_c1 john_Apple21_trs_1 john_Fruit_Apple21_efg_1 john_Fruit_Apple21_tefg_1 ! This ...


2

Other variant with awk awk ' NR==1{ for(i=2;i<=NF;i++){ count++ if($(i-1)!=$i || count>4){ D[i]=1 count=0 } } } { for(i in D) $i=" "$i print } ' OFS="" data.file >new.file And sed sed -re ' s/ ...


3

Here's an awk script. You just change the number 5 to have other groupings. awk ' NR==1{ previous = $1 for(i = 1;i<=NF+1;i++) if($i!=previous){ col[++numcol] = i previous = $i } } { j = 1; start = 1 for(i = 1;i<NF;i++){ printf "%s",$i if(i==col[j]-1){printf " "; start = col[j++]} else if((i-start+1)%5==0)printf " " } ...


2

Try this: echo 'word1 word 2 ... word9 - wordA wordB ... wordZ' | sed 's/\(.*\) - \(.*\)/\2 - \1/' Output: wordA wordB ... wordZ - word1 word 2 ... word9


1

This is similar to choroba's answer $ cat file 1 1 1 1 1 1 1 1 2 2 2 2 3 3 3 3 3 3 2 4 5 8 9 10 13 17 19 29 30 32 33 50 700 800 900 950 $ perl -ane ' if ($. == 1) { for (($n,$i,$j) = (1,0,0); $j < @F; $n++, $j++) { if ($n == 3 || $F[$j] != $F[$j+1]) { $i-- if $i == $j; push @pairs, [$i, $j]; ...


3

@cuonglm's comment illustrates how you can do this with sed: sed -e 's/.*\(Apple\)/\1/' input.txt You might also consider using good old grep: grep -o 'Apple.*' input.txt Caveat 1 - I don't have any solaris handy, so it might be that the solaris grep doesn't have the -o option. YMMV. Caveat 2 - If your input file has lines that don't contain any ...


4

Just pipe the output to column -t: sed 's/$/ IN A 192.168.155.128/' db.malware-host-only | column -t > tmp mv tmp db.malware-host-only You can use the -o parameter to extend the spaces between columns: column -t -o ' ' You can't use sed -i with that, so output to a temp file, then rename it to the original name.


1

awk '{ printf "%-30s%10s%10s%20s\n",$1,"IN","A","192.168.100.10"; }' file


4

Perl to the rescue! #!/usr/bin/perl use warnings; use strict; my $group_size = 3; my @first = split ' ', <>; my @groups; my $start_index = 0; while ($start_index < @first) { my $step = 1; while ( $step < $group_size && $start_index + $step < @first && $first[$start_index] == $first[ ...



Top 50 recent answers are included