Tag Info

Hot answers tagged

8

Inside [...], backslash is not special. [\[] matches both backslash and [1. If you want to include the ] character in the set, you have to make sure it's first: []X] matches ] or X while [X]] would match X followed by ] (and [X\]] would match X or \ followed by ]). To exclude it, it has to be right after ^: [^]X] is any character but ] or X. So, in your ...


8

You have a space after \1 in your replacement, just remove that and you should be good perl -i -p -e "s/^(password[]*=[ ]*).*$/\1$passwd/" config.properties ^ Removed space here


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

That's a typical job for tr: LC_ALL=C tr '\0-\10\13\14\16-\37' '[ *]' < in > out In your case, it doesn't work with sed because you're in a locale where those ranges don't make sense. If you want to work with byte values as opposed to characters and where the order is based on the numerical value of those bytes, your best bet is to use the C locale. ...


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


5

You don't need to loop, you can tell cat to read all the files: cat /var/abc/*.csv > file1.csv && rm /var/abc/*.csv as long as there aren't too many files (but the limit is huge). Using && between the two commands ensures the files are only deleted if they were successfully "copied". There are a couple of caveats though: you mustn't ...


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

Different tools and versions thereof support different variants of regular expressions. The documentation of each will tell you what they support. Standards exist so that one can rely on a minimum set of features that are available across all conforming applications. For instance, all modern implementations of sed and grep implement basic regular ...


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"


4

sed -i '$a gem '"'"'forum2discourse'"'" Gemfile Alternate Solution If you wish to do it your way, then use the bash $'string' format. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. sed -i $'$a gem \'forum2discourse\'' Gemfile Source: ...


4

The Theory The rules are : inside a ' delimited string, nothing gets interpreted and anything but a ' doesn't have special meaning. This means that only a ' need escaping but it also mean that, in order to escape it, you need the '\'' construct. (The first ' ends the string, the following \' adds a literal ' (the escape prevents the start of a new string) ...


4

If you reverse the file, you can add a line the first time you see something: tac lists.txt | awk -v l1="list1" -v val1="something new" \ -v l2="list2" -v val2="another thing" ' index($0, l1"[i++]") && !found1 { printf "%s[i++] = \"%s\";\n", l1, val1 found1 = 1 } index($0, l2"[i++]") ...


4

Try using awk: awk -F'"' '{ print $2 }' conf.txt


3

sed uses regular expressions. These are different from patterns ("globs") that the shell uses. Notice that the following doesn't work: $ echo hostname=abc | sed "s/\<hostname=*\>/hostname=int1/" hostname=int1=abc But, the following does: $ echo hostname=abc | sed "s/\<hostname=.*\>/hostname=int1/" hostname=int1 You need a . before the *. ...


3

Once in visual block mode with your lines selected try this: :'<,'>s/cron/at/g Vim has search and replace capability without the need to call an external command.


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

You are correct - \w is part of PCRE - perl compatible regular expressions. It's not part of the 'standard' regex though. http://www.regular-expressions.info/posix.html Some versions of sed may support it, but I'd suggest the easiest way is to just use perl in sed mode by specifying the -p flag. (Along with the -e). (More detail in perlrun) But you don't ...


3

It would be possible to use sed for this but awk is more natural: awk -F'"' -v OFS='"' '$8 {cmd="date -d \""$8"\" +%FT%T%z"; cmd | getline $8; close(cmd)} 1' input.json {"_id":"","timestamp":"2015-04-20T01:30:55-0700"} {"_id":"","timestamp":"2015-04-20T01:32:25-0700"} {"_id":"","timestamp":"2015-04-20T01:35:39-0700"} The above show an offset of -7:00 ...


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



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