Tag Info

Hot answers tagged

17

Using paste: paste -d \\n file2 file1


14

Here are a few ways: Another awk approach awk '{$2+=$3;}NF--' file Perl perl -lane 'print "$F[0] ",$F[1]+$F[2]' file or perl -ape 's/$F[1].*/$F[1]+$F[2]/e' file Shell (much slower/less efficient than the above) while read a b c; do echo "$a $((b + c))"; done < file


14

With POSIX paste: paste -d'\0' file1 file2 > new_file With paste from GNU coreutils, you can use -d ''.


11

You can try using awk: awk '{ print $1, $2 + $3; }' /tmp/raw Result will be (I suppose value for 2015-03 should be 10000): 2015-01 6000 2015-02 8000 2015-03 10000


10

I'd use awk, but not store the whole content of L.txt in memory and do unnecessary hash look ups ;-). list=L.txt file=F.txt LIST="$list" awk ' function nextline() { if ((getline n < list) <=0) exit } BEGIN{ list = ENVIRON["LIST"] nextline() } NR == n { print nextline() }' < "$file"


10

Your problem is that by filtering on raw bytes in a UTF-8 character stream, you're eating part of a unicode sequence in a UTF-8 file, resulting in an invalid byte sequence. That can't work. Instead, you need to use a tool that understands UTF-8, and apply a filter on the unicode data, rather than the raw bytes. Since I don't know which implementation of awk ...


9

With GNU sed​: sed 's/;/|/2g' Which globally replaces ; with | starting from the 2nd occurrence. While sed 's/;/|/2 and s/;/|/g are POSIX, the combination is not and the behaviour varies across implementations. With the GNU implementation of sed however, the behaviour is clearly documented.


8

I'd use awk: awk 'NR==FNR {a[$1]; next}; FNR in a' L.txt F.txt Update: I've done performance measures; it seems this version scales even better with very large data sets (as is the case with the stated requirements), since the comparison is very fast and overcompensates the effort necessary to build up the hash table.


8

grep -n | sort | sed | cut ( export LC_ALL=C grep -n '' | sort -t: -nmk1,1 ./L - | sed /:/d\;n | cut -sd: -f2- ) <./F That should work pretty quickly (some timed tests are included below) with input of any size. Some notes on how: export LC_ALL=C Because the point of the following operation is to get the entire file of ./F stacked ...


7

Python comes out fairly concise, and the code Does What It Says On The Tin: python -c "import sys; print min(sys.stdin, key=len)," The final comma is obscure, I admit. It prevents the print statement adding an additional linebreak.


7

You can give grep a file containing a list of patterns to match (or not), and reverse the match: grep -vFx -f file2.txt file1.txt


7

sed 'y/|;/\n|/;s/|/;/;y/\n/|/' <<\IN Question ipsun; option 1 ; option 2 ; option 3 ; option 4 ; ... ; option n IN Note that this does not use a regexp to handle the majority of the replacements, but rather uses a more basic (and far more performant) translation function to do so - and does so in a POSIX portable fashion. This should work on any ...


7

With C omitting meaningful error messages: #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { FILE *L; FILE *F; unsigned int to_print; unsigned int current = 0; char *line = NULL; size_t len = 0; if ((L = fopen(argv[1], "r")) == NULL) { return 1; } else if ((F = fopen(argv[2], ...


6

If portability across unices is a concern, use ed: ed file <<END 1s/^/insertedtext/ w q END


6

Tell awk to print between the two delimiters. Specifically: awk '/\*{4,}/,/<np>/' file That will also print the lines containing the delimiters, so you can remove them with: awk '/\*{4,}/,/<np>/' file | tail -n +2 | head -n -1 Alternatively, you can set a variable to true if a line matches the 1st delimiter and to false when it matches the ...


6

That's the job for uniq: LC_ALL=C uniq file GNU uniq in some locales can report first of sequences of lines that sort the same. Using LC_ALL=C forced bytes comparison behavior, give you persistent result.


6

There are two basic approaches one can use when dealing with fields: i) use a tool that understands fields; ii) use a regular expression. Of the two, the former is usually both more robust and simpler. Many of the commonly available tools on *nix are either explicitly designed to deal with fields or have nifty tricks to facilitate it. 1. Use a tool that ...


6

You could use grep with -A and -B to print exactly the parts of the file you want to exclude but add the -n switch to also print the line numbers and then format the output and pass it as a command script to sed to delete those lines: grep -n -A1 -B2 PATTERN infile | \ sed -n 's/^\([0-9]\{1,\}\).*/\1d/p' | \ sed -f - infile Another way with comm: comm ...


6

don's might be better in most cases, but just in case the file is really big, and you can't get sed to handle a script file that large (which can happen at around 5000+ lines of script), here it is with plain sed: sed -ne:t -e"/\n.*$match/D" \ -e'$!N;//D;/'"$match/{" \ -e"s/\n/&/$A;t" \ -e'$q;bt' -e\} \ ...


5

In addition to @terdon's answer, with awk (and sed) you can use range pattern: awk '/sep1/,/sep2/{print}' file or sed -n '/sep1/,/sep2/p' file will print everything (including) sep1 and sep2. That is: ~$ awk '/sep1/,/sep2/{print}' file sep1 thingsIwantToRead1 thingsIwantToRead2 thingsIwantToRead3 sep2 In your case: ~$ awk '/\*\*\*/,/^$/{print}' ...


5

For your example: dir=$(mktemp -d) sed 's|\t|/|g' file | while read -r line; do mkdir -p "$dir/$line"; done (cd "$dir"; tree) rm -r "$dir" Output: . ├── AMERICA │   ├── CANADA │   │   ├── TORONTO │   │   │   ├── UT-876 │   │   │   └── UT-877 │   │   └── VANCOUVER │   │   ├── UT-871 │   │   ├── UT-872 │   │   └── UT-873 │   ├── MEXICO │   │   ...


5

For the case above, you can do it like this: gsed 's/|/1)/; s/|/2)/; s/|/3)/; s/|/4)/; s/|/5)/' Example: $ echo '| | | | |' | sed 's/|/1)/; s/|/2)/; s/|/3)/; s/|/4)/; s/|/5)/' 1) 2) 3) 4) 5) This works if you can estimate in advance the maximum number of | on a line, and add s/|/N)/ accordingly. If you can't estimate the maximum number of | on a line ...


5

exec 3<file1 exec 4<file2 while read line1 <&3 && read line2 <&4 do echo "line1=$line1 and line2=$line2" done exec 3<&- exec 4<&- Discussion In the above, leading and trailing white space is stripped from the input lines. If you want to preserve this whitespace, replace read … with IFS= read … In the ...


5

Another perl: perl -pe 'BEGIN { binmode \*STDOUT } chomp; tr/AB/\0\1/; $_ = pack "B*", $_' Proof: $ echo ABBBAAAABBBBBABBABBBABBB | \ perl -pe 'BEGIN { binmode \*STDOUT } chomp; tr/AB/\0\1/; $_ = pack "B*", $_' | \ od -tx1 0000000 70 fb 77 0000003 The above reads input one line at a time. It's up to you to make sure the lines are exactly what ...


5

there is a missing -e before s/foo/bar/ (*) there is a confusion, are you (the script) editing index or www/index.js ? if index is a template file (with API_CONTEXT_URL) to be used to produce www/index.js, I would suggest sed -e s,API_CONTEXT_URL,http://localhost:5557,g index > www/index.js note that you can use any chat a separator between ...


4

Perl to the rescue: perl -ne 'if (/\\$/) { $l .= $_ } else { print $l, $_ if $l =~ /XXX/; $l = ""; }' foo.txt $l works as an accumulator. -n processes the input line by line (cf. sed), if the line ends in a backslash, it's added to the accumulator, if not, the accumulator plus the line is printed provided it matches ...


4

With POSIX sed: $ sed -e ' :1 /\\$/{N s/\n// t1 } /\\/!d s/\\[[:blank:]]*//g ' file


4

With GNU sed: sed -i '1s/^/insertedtext/' file This replaces the beginning of the first line with the inserted text. -i replaces the text in file rather than sending the modified text to the standard output. (Thanks to glenn jackman for suggesting dropping the capture I had previously.)


4

POSIX one: $ { printf %s insertedtext; cat <./input_file; } >/tmp/output_file $ mv -- /tmp/output_file ./input_file



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