Tag Info

Hot answers tagged

17

If your text is in an environment variable called STRING, you can do this in a bash shell: echo "${STRING:(-3)}"


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


15

Why has nobody given the obvious answer? sed 's/.*\(...\)/\1/' … or the slightly less obvious grep -o '...$' Admittedly, the second one has the drawback that lines with fewer than three characters vanish; but the question didn’t explicitly define the behavior for this case.


13

In bash and zsh you can do: cat file1 <(echo '********************************') file2 or as mikeserv indicated in his comment (in any shell): echo '********************************' | cat file1 - file2 and in Bash as David Z commented: cat file1 - file2 <<< '********************************' Any newlines in the files will be shown. If ...


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


10

Keeping it simple - tail We should not need a regular expression, or more than one process, just to count characters. The command tail, often used to show the last lines of a file, has an option -c (--bytes), which seems to be just the right tool for this: $ printf 123456789 | tail -c 3 789 (When you are in a shell, it makes sense to use a method like in ...


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


6

Well, I can think of a couple of simple ways but neither involves grep (which doesn't do substitutions anyway) or sed. Perl To replace each occurrence of "line"\n<second> with other characters, use: $ perl -00pe 's/"line"\n<second>/other characters /g' file first other characters line and so on Or, to treat multiple, consecutive occurrences ...


6

read the whole file and do a global replacement: sed -n 'H; ${x; s/"line"\n<second>/other characters /g; p}' <<END first "line" <second> line followed by "line" <second> and last END first other characters line followed by other characters and last


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.


5

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


5

If the string is in a variable you can do: printf %s\\n "${var#"${var%???}"}" That strips the last three characters from the value of $var like: ${var%???} ...and then strips from the head of $var everything but what was just stripped like: ${var#"${var%???}"} This method has its upsides and downsides. On the bright side it is fully POSIX-portable ...


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

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


4

Using awk: awk '{ print substr( $0, length($0) - 2, length($0) ) }' file ted 654 789


4

You haven't said which cut you're using, but since you've mentioned the GNU long option --characters I'll assume it's that one. In that case, note this passage from info coreutils 'cut invocation': ‘-c character-list’ ‘--characters=character-list’ Select for printing only the characters in positions listed in character-list. The same as -b for now, ...



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