New answers tagged

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


3

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


3

I'm not aware of any awk implementation that has support for that. You could write an extension for gawk for that, but here, I'd rather switch to another language. perl makes it easy to convert awk scripts with its a2p script. For instance, if you have an awk script like: {count[$0]++} END { for (i in count) printf "%5d %s\n", count[i], i } a2p on it ...


-1

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


0

awk was first released in the late 70s in Unix V7. Since then, it has undergone significant changes, some of them not backward compatible. The GNU awk manual has a very informative section on the subject. Like for many other utilities, Solaris (contrary to most other Unices) has taken the stance of sticking with the older obsolete implementation for its ...


0

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


0

Other answers, using \s+ are over-matching! \s matches spaces, too, which occur in the data. What you are looking for is "Field_not_containing_TAB" followed by "Tab", followed by "Field_not_containing_TAB". or, in regexp-speak, egrep '^[^Tab]+Tab[^Tab]+$' file.txt Note: there should be no Space characters between the ''s in my egrep rexexp. You see them ...


0

For you, 2 columns is: something[TAB]something, something can have a space in it. So just tell awk your Field Separator is a tab: awk -F'\t' '(NF==2)' to find out the others : awk -F'\t' '(NF !=2) { print "line: " NR " does not have 2 columns: " $0 ;}' Edit : you seem to "mislead" about the file's structure : you said it will contain either 1 word, ...


3

Using grep: grep -E '^[^\s]+\s+[^\s]+$' file.txt [^\s]+ will contain the first column, then any number of white spaces \s+, followed by the second (last) column ^ indicates the start and $ indicates the end of the line POSIX-ly: grep '^[^[:blank:]]\+[[:blank:]]\+[^[:blank:]]\+$' file.txt


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. ...


2

How many lines are in each file. Use wc, originally for word count, I believe, but it can do lines, words, characters, bytes, and the longest line length. The -l option tells it to count lines. wc -l <filename> This will output the number of lines in : $ wc -l /dir/file.txt 32724 /dir/file.txt You can also pipe data to wc as well: $ cat ...


0

Depends how precisely you want the formatting - usually tab separated is enough - but I'd tackle it like this: #!/usr/bin/env perl use strict; use warnings; #set record separator to double line feed. local $/ = "\n\n"; #print header row print join "\t", "VM", "Virtual_Disk", "size", "Physical Disks", "\n"; #iterate stdin or files specified on ...


0

perl was designed and made for precisely this kind of problem. It's nickname was "pathological eclectic rubbish lister". Read the documentation on perlform. What you do is read your data (say, line at a time), parse it, stuff into variables, then issue the command write which outputs the current data based on a format which you defined. For your data you ...


0

you can try the same windows like fc command in Unix and Linux i.e. diff <file_new> <file_old> command. Line with +++ or --- in front of them have changed and one with no +'s and -'s haven't changed Lines with - sign are removed from the new file however they existed in old version Lines with + sign are added from in new file however they ...


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 ...


0

Besides that awk(same as and another text processors) is not a right xml-parsing tool: awk ' lines{ lines=lines "\n" $0 } /<\/record/{ if(lines ~ /keyword>SEARCH</) print lines lines="" } /<record/{ lines=$0 } ' <input.txt >output.txt Same as sed sed -n ...


1

If I have understood correctly, this might be a solution in awk!: /^<record/ { x1=""; while (match($0, "record>$")==0) { x1=x1 $0"\n"; getline; } x1=x1 $0; if (x1 ~ />SEARCH</) { print x1 > "output.txt"; } } This will extract the blocks, record> to \record>, containing the key "SEARCH" ...


5

I'm going to assume that what you've posted is a sample, because it isn't valid XML. If this assumption isn't valid, my answer doesn't hold... but if that is the case, you really need to hit the person who gave you the XML with a rolled up copy of the XML spec, and demand they 'fix it'. But really - awk and regular expressions are not the right tool for the ...


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


1

I had to run dos2unix on all the infiles and everything worked fine after that.


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 : ...


0

Command: awk 'NR==FNR {values[NR]=$1; next;} { if (values[1]>values[2]) newvalue=0; else newvalue=1; print $0 "," newvalue; }' b.csv a.csv Output: *,*,*,1,1 *,*,*,2,1 *,*,*,3,1


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

First, make sure you are using spaces and not tabs in the input file. Then, try separating the variables by commas, like this: awk '{print $2,$3,$1}' infile.xyz > output.xyz If you place the variables side by side, like in $2 $3 $1, awk concatenates them. Also, you may want to try the printf function, as it provides better formatting capabilities. ...


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, ...


2

The best tool for the job is probably join but since you mentioned awk, here's another approach: $ awk -F',' -vOFS="," 'FNR==NR{a[$1]=$2;b[$1]=$3; next}{print $0,a[$3],b[$3]}' file1 file2 Converting,:Developer::|[E],android,Exact,,,,8,31,1,2 Converting,:Developer::|[E],osx,Exact,,,,8,31,2,5 Converting,:Developer::|[E],windows,Exact,,,,8,31,, It uses two ...


3

You could use join for this join -1 1 -2 3 -t ',' -a 2 -o 2.{1..9} 1.{2..3} <(sort file1.csv) <(sort file2.csv) -1 and -2 specifies which field from the files to compare -t specifies the seperator to use for the fields -a 2 says to print lines in <file2> that do not match -o configures the output based on <file>.<field>


0

already answered partially here -- http://stackoverflow.com/questions/24516141/awk-processing-2-files-with-different-field-separators awk 'NR==FNR {arr[$2]=$2FS$3FS$4FS$5FS$6; next} {print $1,arr[$3]}' file1 FS="," file2 1 rs3094315 0 0 C T


3

My first thought was the same as Jeff Schaller's: cut -c 17-64. However that includes the closing quote, so you need cut -c 17-63 If you want the first quoted word and not every word is the same length, you can use awk: awk -F\" '{print $2}' file.txt


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 ...


2

pssh makes this much easier, but for your simple use case ssh will also work. While what you have above could have worked, provided the server is set up to run a command and exit on login (which is somewhat unlikely) you probably meant something like this: ssh $i <command> | grep tsm | ... If you really need to check a login banner for tsm, try ...


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

You can make use of NR and FNR in awk. awk 'FNR==NR { _a[FNR]=$3;} NR!=FNR { $4 += _a[FNR]; print; }' file1 file2


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 ...



Top 50 recent answers are included