Hot answers tagged

24

In sed: -i option will edit the input file in-place '1d' will remove the first line of the input file Example: % cat file.txt foo bar % sed -i '1d' file.txt % cat file.txt bar Note that, most of the time it's a good idea to take a backup while using the -i option so that you have the original file backup up in case anything goes wrong in ...


9

The right tool for this job is column. You can specify column separator with -o (on OS X it's -s) , e.g.: column -t -o ' ' file gives TLRUIDA CBdms Status DP 6/1/1 DC 6/1/5 0 Y TLRUIDA CBdms Status DP 6/2/1 DC 6/2/5 0 Y TLRUIDA CBdms Status DP 6/3/1 DC 6/3/5 0 Y TLRUIDA CBdms Status DP 6/4/1 ...


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";


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


5

1. a ) sed '1d' file.txt Prints the contents of file.txt; excluding the first line; to the standard output. 2. a ) sed -i '1d' file.txt Prints the contents of file.txt; excluding the first line; back into file.txt; overwriting it's former self. In some cases, you may be required to create a backup. 2. b ) sed -i backup.txt '1d' file.txt ...


5

Using gsub: awk '{gsub(/\"|\;/,"")}1' file chr1 134901 139379 - ENSG00000237683.5 chr1 860260 879955 + ENSG00000187634.6 chr1 861264 866445 - ENSG00000268179.1 chr1 879584 894689 - ENSG00000188976.6 chr1 895967 901095 + ENSG00000187961.9 If you want to operate only on the fifth field and preserve any quotes or semicolons ...


5

To explicitly delete everything that comes after ".com", just tweak your existing sed solution to replace ".com(anything)" with ".com": sed 's/\.com.*/.com/' file.txt I tweaked your regex to escape the first period; otherwise it would have matched something like "thisiscommon.com/something". Note that you may want to further anchor the ".com" pattern ...


5

EDIT: Extensible to any number of output rows, in a simple one-liner for loop: for ((i=1;i<=2;i++)); do cut -d: -f "$i" input | paste -sd: ; done | column -t -s: Original answer: You can do this as a one-liner using bash process substitution: paste -sd: <(cut -d: -f1 input) <(cut -d: -f2 input) | column -t -s: The -s option to paste makes ...


5

If you have the rs (reshape) utility available, you can do the following: rs -Tzc: < input.txt This gives the output format exactly as specified in the question, even down to the dynamic column widths. -T Transposes the input data -z sizes the columns appropriately from the max in each column -c: uses colon as the input field separator This works ...


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


5

You have to quote strings with spaces when you use sed (or most other tools) from the commandline. And since you already use the double quote, you have to go for single quotes: echo '"GET /images/loading.gif HTTP/1.1" 200 10819 "https://...' | \ sed 's|HTTP/1.1" 200.*|HTTP/1.1" 200|' gives: "GET /images/loading.gif HTTP/1.1" 200


4

Oneliner in Perl (thanks terdon!): perl -0 -pe 's/\s+(\S+)-(<\/page>\s+<page>)(\S+)/$2$1$3/g' filename What it does: matches against regular expression, and uses parts matched to reconstruct your word.


4

sed will work, but this is much simpler with tr, which translates characters. for file in *.php; do tr 'ăâîșțĂÂÎȘȚ' 'aaistaaist' < "${file}" > "translated_${file}" The sed equivalent (which you'll need on GNU systems, not OS/X where tr doesn't support multibyte characters) would be: sed 'y/ăâîșțĂÂÎȘȚ/aaistaaist/'


4

Sed processes the input line by line. It's easier in Perl that can process the whole file at once: perl -0777 -pe 's=<script>.*?\n.*?</script>==sg' -0777 reads the whole file ? after * makes it "frugal", i.e. it matches the shortest possible string. /s makes . match a newline which it normally doesn't. Note that it can break if the script ...


4

With sed you can select ranges and delete them: sed '/<script/,/<\/script>/d' inputfile


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


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.


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


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


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


4

One way: cat -s file | sed 's/^$/---/' From man page of cat : -s, --squeeze-blank never more than one single blank line Once cat has squeezed the blank lines, sed replaces the blank with with a ---


4

You could add word boundaries, and change . to \. (so as to match literal periods, instead of any characters) sed 's/\b72\.16\.90\.12\b/#&/g' Also note the use of & to save having to duplicate the replacement. You probably don't need the g modifier either in this context, since your host addresses are one per line.


3

Using sed to remove all instances of '";': sed -i 's/[";]//g' file To only remove from 5th column sed is probably not the best option.


3

In this particular case, it will be enough to just change all occurrences of two or more spaces to a tab: sed 's/ */\t/g' file For a more general solution, you can make sure each column is printed with the right width using printf. You can do this directly in the shell: $ while read line; do printf '%-8s%-6s%-7s%-3s%-8s%-3s%-7s%-2s%-2s\n' $line; ...


3

If you have the rename implementation with Perl regexes (as on Debian/Ubuntu/…, or prename on Arch Linux), you need $1 instead of \1. Also, no backslashes on capturing parentheses: rename 's/(.*)_(.*)/$2_$1/' *_* If not, you have to implement it yourself. #! /bin/bash for file in *_* ; do left=${file%_*} right=${file##*_} mv "$file" ...


3

If your data is formatted exactly as shown (i.e. no other " or ; in other columns that need to be preserved), then you can simply use tr to remove these characters: tr -d '";' < input.txt > output.txt


3

The standard delimiter used in sed commands is /, as in commands like this: sed -e s/foo/bar/g < input > output However, if the s command is followed by a different character, that becomes the delimiter for that particular expression. The use of non-/ delimiters is common when the delimiter itself needs to appear in the command, and so would ...


3

Some kind of a monster) With perl it should be easier cat file ba bla bla hyphe-</page> <page>nated bla bla bla and the output should look like bla bla bla</page> <page>hyphenated bla bla bla It's GNU sed (in some other sed-s -E option is used for extended regular expressions) sed -nr '/[[:alpha:]]+-<\/[[:alpha:]]+>$/{ N ...


3

Just delete everything between the first line and the first pattern, between the second pattern and the last line: apt-cache showpkg gdm | sed '1,/Reverse Depends:/d;/Dependencies:/,$d' Note that this approach will fail if your first pattern occurs on the first line and doing regexes checking more than is necessary can affect performance. A better ...


3

The best tool for non-interactive in-place file editing is ex. ex -sc '%s/\(\.com\).*/\1/ | x' file.txt If you've used vi and if you've ever typed a command that begins with a colon : you've used an ex command. Of course many of the more advanced or "fancy" commands you can execute this way are Vim extensions (e.g. :bufdo) and are not defined in the ...



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