Tag Info

Hot answers tagged

16

You can use grep. Give it the small file as input and tell it to find non-matching lines: grep -vxFf file.txt bigfile.txt > newbigfile.txt The options used are: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) ...


11

Part 1 Simply delete the 13th line: sed '13d' <file.txt And a general way to do the complement of the above is: sed '13!d' <file.txt Part 2 Because it can be done: sed -n ':a;${P;q};N;4,$D;ba' <file.txt Note the 4 is one more than the number you require. So if you wanted the last-10th line, this would be 11. Testing with seq: $ seq 100 ...


8

comm is your friend: NAME comm - compare two sorted files line by line SYNOPSIS comm [OPTION]... FILE1 FILE2 DESCRIPTION Compare sorted files FILE1 and FILE2 line by line. With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and ...


8

This will do it. There's probably a better way, but this is the first approach that came to mind: echo 'Sentence #1 (n tokens): Blah Blah Blah [... ... ...] ( ... (other possible parens here) ) (...) (...) Sentence #2 (n tokens): ' | perl -0777 -nE ' $wanted = 2; $level = 0; $text = ""; for $char (split //) { $level++ if ...


7

I wrote a small script to do this called he, e.g. he apt-get autoclean. The basic strategy is: search for the word (e.g. autoclean) as the first word on a line, then print until the next blank line. You can get something similar using basic sed, e.g. man apt-get | sed -ne '/^ *autoclean/,/^$/p' You can find the script on my github page (linked above) ...


7

Found these Python one liners that do what you want: $ alias urldecode='python -c "import sys, urllib as ul; \ print ul.unquote_plus(sys.argv[1])"' $ alias urlencode='python -c "import sys, urllib as ul; \ print ul.quote_plus(sys.argv[1])"' Example $ urldecode 'q+werty%3D%2F%3B' q werty=/; $ urlencode 'q werty=/;' q+werty%3D%2F%3B References ...


7

With awk: ps -af -u sas | awk 'BEGIN {RS=" "}; /-Dapp.name/'


6

There is a built-in function for that in the Python standard library. In Python 2, it's urllib.unquote. decoded_url=$(python2 -c 'import sys, urllib; print urllib.unquote(sys.argv[1])' "$encoded_url") Or to process a file: python2 -c 'import sys, urllib; print urllib.unquote(sys.stdin.read())' <file >file.new && mv -f file.new file In ...


5

If you have a version of grep that supports -P (perl-compatible regex, PCREs) and -o (print only the matching string), you can do df | grep -oP '/sda3.* \K\d+(?=\s+\d+%)' Explanation Here, we match /sda3, then as many characters as possible until we find a stretch of numbers (\d+) which is followed by one or more spaces (\s+), then one or more numbers ...


5

$ awk '/SETTINGS START/,/SETTINGS END/ {if (FNR==NR) {a=a"\n"$0}} FNR==NR{next} /SETTINGS START/{print substr(a,2)} /SETTINGS START/,/CUSTOMIZATION SETTINGS END/{next} 1' file2 file1 ANJALI NISHA // +++ CUSTOMIZATION SETTINGS START +++ WELCOME ALL // +++ CUSTOMIZATION SETTINGS END +++ PREETI MONA Explanation awk implicitly loops through files line ...


5

GLib's GKeyFile class could be what you are looking for.


5

To add all lines from filename to filename1 using sed command you can do the following: sed r filename1 filename Please note however that the result will be slightly different from the output in your question, namely: Name Class1 Class2 Class3 Lee 92 94 88 Chancy 91 85 95 Dora 99 77 96 Jefferry 84 98 90 Cindy ...


5

If the only problem is that grep treats it as binary, tell grep to search it regardless: $ head /bin/bash > out $ echo "test" >> out $ grep test out Binary file out matches $ grep -a test out test From man grep: -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option.


4

Glenn's answer is good (and probably faster for large input), but for the record, what Glenn proposes is totally possible in bash too. It was a relatively simple matter to port his answer to pure bash in just a few minutes: s='Sentence #1 (n tokens): Blah Blah Blah [... ... ...] ( ... (other possible parens here) ) (...) (...) Sentence #2 (n tokens): ...


4

If you want to simple use sed command then use following command: sed -e 's/%21/!/g' -e 's/%23/#/g' -e 's/%24/$/g' -e 's/%26/&/g' -e s/%27/"'"/g -e 's/%28/(/g' -e 's/%29/)/g' But is is more convenient to create script like (say sedscript): #!/bin/bash s/%21/!/g s/%23/#/g s/%24/$/g s/%26/&/g s/%27/"'"/g s/%28/(/g s/%29/)/g Then run sed -f ...


4

$ awk 'FNR==NR{a[$1]=$2;next} ($1 in a) {print $1,a[$1],$2}' file2 file1 aa 45 32 bb 31 15 cc 50 78 Explanation: awk implicitly loops through each file, one line at a time. Since we gave it file2 as the first argument, it is read first. file1 is read second. FNR==NR{a[$1]=$2;next} NR is the number of lines that awk has read so far and FNR is the ...


4

Pure awk: $ awk -F'[, ]' 'NR==FNR{n[$2]=$1;next}{m[$3]+=n[$1]} END{for(i in m){print i " " m[i]}}' \ file1 file2 degree1 2 degree2 5 Or you can put it into a script like this: #!/usr/bin/awk -f BEGIN { FS="[, ]" } { if (NR == FNR) { n[$2] = $1; next; } else { m[$3] += n[$1]; } } END { for (i in m) { ...


4

The command paste combines lines from multiple input files into one file with multiple columns, one from each file. It can be used to combine lines of the same file into a file with multiple columns, such that consecutive lines end up in consecutive columns in the output. The idea is to use one input multiple times for each row, instead of multiple inputs. ...


4

It might answer your question, so here are the results of a few tests I just ran: $ > output.txt $ file output.txt output.txt: empty $ echo "" > output.txt $ file output.txt output.txt: very short file (no magic) $ echo " " > output.txt $ file output.txt output.txt : ASCII text As you can see, the file isn't categorised the same way according ...


4

ps -af -u sas | sed -n '/[j]ava/s/.*-Dapp\.name=\([^ ]*\).*/\1/p'


4

You're not showing the error message you are getting but it's probably grep: unknown devices method That's because, like all or at least most other command line programs, grep assumes that anything that starts with a - is an option and tries to parse it as such. In this case, -D is used to instruct grep on how to deal with a device file (see man grep ...


4

The following example counts the times I am mentioned without needing the lastloggedin file: $ last | awk '$1=="yeti" { ++count } END { print count }' 106 If you insist in using or are forced to use the lastloggedin file, you can do it this way: $ last > lastloggedin $ awk '$1=="yeti" { ++count } END { print count }' lastloggedin 106 Use ...


3

Standard approach is to use temporary files, but you can change files in place if you enclose cat in $() parenthesis: for file in *.txt; do echo "$file"$'\n'"$(cat -- "$file")" > "$file"; done


3

If you don't need to keep the whitespace exactly the same, this works: $ perl -lane 'do{$_=sprintf("%.8f",0) for @F[3..6]} if $F[6]>1; print "@F"' file 0.04718683 0.24081557 0.02831210 0.00000000 0.00000000 0.00000000 0.00000000 0.05662420 0.24081557 0.02831210 -0.00006975 0.00001697 -0.00144172 0.00144350 0.06606157 0.24081557 0.02831210 0.00039672 ...


3

How to use grep to solve it as you ask is already anwered; This answer is about how to solve the example problem - getting a separate value from df: GNU df from coreutils has an option to specify the columns shown in the output: $ df --output=avail /dev/sda3 Avail 9816416 Unfortunately, there is no option to suppress the column header - so it needs to ...


3

Use tee and redirect it to stderr my_command | tee /dev/stderr | grep -q '^Error' It will save grep exit status and duplicate all the output to stderr which is visible in console. If you need it in stdout you can redirect it there later like this: ( my_command | tee /dev/stderr | grep -q '^Error' ) 2>&1 Note that grep will output nothing, but it ...


3

If doing the comparision line-by-line is acceptable, then the following will tell which lines are duplicated in file text and how many times each one appears: sort text | uniq -c | grep -vE '^\s*1 ' As an example, $ cat text alpha beta alpha gamma alpha beta $ sort text | uniq -c | grep -vE '^\s*1 ' 3 alpha 2 beta Using the usual unix ...


3

$ top -bn1 | grep free KiB Mem: 8117084 total, 6578888 used, 1538196 free, 302216 buffers KiB Swap: 8060924 total, 26004 used, 8034920 free, 1564856 cached $ top -bn1 | grep -oP '\S+(?=\s+free)' 1544132 8034920 requires GNU grep, but you've tagged "linux", so you're OK For just the "Mem": top -bn1 | grep -oP 'Mem.*\s\K\S+(?=\s+free)'


3

Let's start with a sample input file: $ cat dump.txt some: other email: proxy2@domain.com fullName: LDAP Proxy2 gui: 987 other: thing To extract the desired lines and reformat them as CSV: $ awk '/email:|fullName:|gui:/{s=s", "$0} END{print substr(s,3)}' dump.txt email: proxy2@domain.com, fullName: LDAP Proxy2, gui: 987 How it works: ...


3

And another Perl approach: #!/usr/bin/env perl use URI::Encode; my $uri = URI::Encode->new( { encode_reserved => 0 } ); while (<>) { print $uri->decode($_) } You will need to install the URI::Encode module. On my Debian, I could simply run sudo apt-get install URI::Encode Then, I ran the script above on a test file containing: ...



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