Tag Info

Hot answers tagged

16

id also accepts paramters, so you don't have to grep for it (-g to print only the group, and -n to print names instead of ids): $ id -gn usera groupa To save that into a variable use that: groupname=$(id -gn usera)


7

With awk you can do: awk '{print >out}; /XYZ/{out="file2"}' out=file1 largefile Explanation: The first awk argument (out=file1) defines a variable with the filename that will be used for output while the subsequent argument (largefile) is processed. The awk program will print all lines to the file specified by the variable out ({print >out}). If the ...


7

Using awk, split the file using tabs and output the first field in full and the first 75 characters (at most) of the second: awk -F "\t" 'BEGIN { OFS=FS }; { print $1, substr($2, 1, 75); }' As pointed out by fedorqui, you can handle files with more than two fields by replacing the fields you need to truncate: awk -F "\t" 'BEGIN { OFS=FS }; { ...


6

With a modern ksh here's a shell variant (i.e. without sed) of one of the sed based answers above: { read in <##XYZ ; print "$in" ; cat >file2 ;} <largefile >file1 And another variant in ksh alone (i.e. also omitting the cat): { read in <##XYZ ; print "$in" ; { read <##"" ;} >file2 ;} <largefile >file1 (The pure ksh ...


5

To extract lines that start with May 1: grep "^May 1\b" file Or: sed -n '/^May 1\>/p' file Or: awk '/^May 1\>/' file The above two assume a tool, such as the GNU awk or sed, that supports \> as a word boundary regex. The purpose of the word boundary is to prevent the regex from matching, for example, May 10. More If you are looking for ...


5

{ sed '/XYZ/q' >file1; cat >file2; } <infile With GNU sed you should use the -unbuffered switch. Most other seds should just work though. To leave XYZ out... { sed -n '/XYZ/q;p'; cat >file2; } <infile >file1


5

This is a job for csplit: csplit -sf file -n 1 large_file /XYZ/ would silently split the file, creating pieces with prefix file and numbered using a single digit, e.g. file0 etc. Note that using /regex/ would split up to, but not including the line that matches regex. To split up to and including the line matching regex add a +1 offset: csplit -sf file ...


5

You can use back-references: echo A627E39B | sed 's/\(.\)\(.\)/\2\1/g' This finds all occurrences of two characters and swaps them. As glenn jackman pointed out, enabling extended regular expressions (-r on GNU sed, -E on BSD) avoids having to escape the parentheses: echo A627E39B | sed -r 's/(.)(.)/\2\1/g'


5

By GNU sed sed -n '/^name-/{s///;N;s/[a-z].*\s//p}' file JOHN NY TOM TX LILLY LA ROSY WA By GNU awk awk -F'[ -]+' '/name/{a=$2}/state/{print a,$3}' OFS='\t' file JOHN NY TOM TX LILLY LA ROSY WA By grep grep -o '[[:upper:]]\{2,\}' file | paste - - JOHN NY TOM TX LILLY LA ROSY WA


5

I'd use Perl's paragraph mode: pactl list sink-inputs | perl -00ne 'print if s/(.*?VLC.*?\n).*/$1/ms' The -00 sets the input record separator to \n\n so a "line" is a paragraph. Then, the substitution will match everything until the first VLC and then anything until the 1st newline and save them as $1. Everything after that is removed (since we're ...


5

With ed: ed -s <<'IN' r !pactl list sink-inputs /VLC/+,$d ?Sink Input?,.p q IN It reads the command output into the text buffer, deletes everything after the first line matching VLC and then prints from the previous line matching Sink Input up to current line. With sed: pactl list sink-inputs | sed -n 'H;/Sink Input/h;/VLC/{x;p;q}' It appends ...


4

The main problem has already been highlighted by don_crissti in his comment. He determined the file type by running: file koran ... which outputs: ASCII C++ program text, with CRLF, LF line terminators You need to remove the CR (Carriage Return - hex value \x0D) characters which are present at the end of some lines in your input file. This CR is ...


4

paste -d " " file2 file1 Output: 1 1241 aaa 2 301094209 bbbb 3 432423 c -d LIST: reuse characters from LIST instead of TABs


4

Some more choices. I have saved your example text in file for simplicity. grep and PCREs: $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' file purple yes violet affirmative To get them on the same line, just parse. For example $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' | paste -d" " - - – purple yes violet affirmative sed $ sed ...


4

Portably/POSIXly with sed: tab=$(printf '\t') sed "s/\($tab[^$tab]\{0,75\}\)[^$tab]*/\1/" Or to truncate every column: sed "s/\([^$tab]\{75\}\)[^$tab]*/\1/g"


3

Perhaps the command paste is what you are looking for? paste file0 file1 This will print, in sequence, each line of file0, followed by the matching line of file1. The default separetor is a tab, but that can be changed with -d if necessary.


3

The exact equivalent would be something like: sed -n '/email2/{s/^[^=]*=\([^=]*\).*/\1/;p;}' < file But you'd probably want instead: sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file (that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the ...


3

Many grep implementation will use line buffered when standard output is terminal. When standard output is terminal, it's often an interactive session, you want to get data as soon as possible. So grep will write data to standard output as soon as seeing a newline. When standard output isn't terminal (often meaning non-interactive session), grep will use ...


3

The awk equivalent of grep -w Duplications: *.info| grep -v Conditional >dups would be awk '/\<Duplications:/ && !/Conditional/ {print}' *.info > dups If a line matches the word "Duplications:" and the line does not contain "Conditional", print the line. I don't think awk offers any benefits over grep here.


3

html-xml-utils xml2 xml-twig-tools xmlstarlet


3

Try this with GNU sed: sed -n -e '1,/XYZ/w file1' -e '/XYZ/,${/XYZ/d;w file2' -e '}' large_file


3

With GNU sed: $ sed -i.bak '/^[^[:blank:]]/{N;s/\n[[:blank:]]\+//;}' file C1r1 r2 r3 C2r1 r3


3

If you want to print only the first 75 characters of the second column (including spaces, and assuming only two columns in the file), you can do: $ perl -pe 's/(\t.{75}).*/$1/' file XY981743 foobarlkasdf saflkas asfZR!sgfad asdSAD asdsadf SAdfasdf46lk lksad bar fool Or, with GNU sed: $ sed 's/\(.*\t.\{75\}\).*/\1/' file XY981743 foobarlkasdf ...


3

With GNU sed: sed '/binaries=(/,/)/{ //!d /)/e uname }' Replace uname with your command.


3

Use awk: awk '{a[NR]=$0} {if(NF!=1){y=$2;x=$0;b[x]=1;c[x]=1}else{b[x]++;if(y==$1){c[x]++}}} END{for (n in a){z=a[n];print z" "b[z]" "c[z]}}' file Admittedly it isn't the simplest one, but it works for me with your given input. Explanation: {a[NR]=$0}: First load all content of the file in an array called a. if(NF!=1): If it's a row where we have ...


3

As I interpret your question, you're trying to get a simple awk script to run, and then you'll elaborate a ksh script around it. Towards that end, you'll need to modify your script like this: 1 #!/bin/ksh 2 awk 'BEGIN { print "START" } \ 3 { print "Hello World" } \ 4 END { print "STOP" }' 5 6 7 exit 0 Your script will execute awk ...


3

You might want to check this site about diff3 with this program you can compare 3 files as sample output: $ diff3 parent.txt your.txt mine.txt ==== 1:1,2c Hello, This is parent file. 2:1,2c Hello, This is your file. 3:1,2c Hello, This is my file. You can use diff3 file1.txt file2.txt file3.txt > output.txt


3

To select shared lines in all files you can use grep (out.txt) grep -ho ' [0-9] [a-z] ' file3 | grep -Fof - file2 | grep -Ff - file1 select operatable field (as variant cut -d' ' -f3,4 file3) and use it to search it in next file2 and file1. As usual for joining 2 file use join command (surprise!) (out2.txt) join -j 3 <(sort -k3,4 file1 | sed 's/ ...


3

Here's another awk way (which, I now see, is just an uglier version of @Costas's): $ awk -F'[- ]' '($1~/name/){k=$2}($1~/school/){print k,$NF}' file JOHN NY TOM TX LILLY LA ROSY WA You can also use grep: $ grep -oP '^(name-\K\S+|school.*\s+\K.*)' file | paste - - JOHN NY TOM TX LILLY LA ROSY WA In your particular example, of course, you ...


3

Perl one-liner: perl -pe's/\[gene=([^\]]*)\K\]/$h{$1}++?"$h{$1}]":"]"/e' yourfile Explained: -p: execute the code for each line of the file (stored in $_) and print $_ at the end. -e: code. s/regex/replacement/e: Match regex and replace it with replacement on $_. The regex: /\[gene= # match [gene= ([^\]]*) # match anything but "]" and put it a ...



Only top voted, non community-wiki answers of a minimum length are eligible