Tag Info

Hot answers tagged

14

The easiest way is using grep -v command: grep -v "Products below" your_file.csv > new_file.csv


9

With your input data, you can try: $ sed '/^Products/d' file Number, month, year, reference, store 1,1,2014,13322,main 2,2,2014,13322,main 3,3,2011,1322,main 4,4,2012,3322,main 5,4,2013,122,secondary 12,411,2010,122,Albany 25,41,2009,122,Dallas 35,24,2008,122,New Using sed -i.bak to edit the file inplace and create a backup file: sed -i.bak ...


8

A straight Perl solution: $ perl -lne ' if(/^>/) {printf "%s ", $_;next} if(/^$/) {printf "\n";next} printf "%s", $_; ' file >Country1 Australia >Country5 Switzerland >Country2 Netherlands or a shorter way: $ perl -ane 'BEGIN{$/="";};print "$F[0] ",@F[1..$#F],"\n"' file >Country1 Australia >Country5 Switzerland >Country2 ...


7

There's an app for that! $ cat file1 file1 line1 file1 line2 $ cat file2 file2 line1 file2 line2 Now, if you pass these files as arguments to paste: $ paste -d' ' file1 file2 file1 line1 file2 line1 file1 line2 file2 line2 If by "file1 1st line followed by file2 first line, and file1 second line followed by file2 second line etc.", you mean that you ...


7

You really are not going to be able to do this with a simplistic sed script. I’m assuming that you will want to reduce to “citation forms”, collapsing all inflections into a base form. That means that adjectives like protégé, protégés, protégée, protégées all count as the same thing, the base adjective/participle protégé. Similarly, all inflections of ...


7

You should be able to use sed -e :a -e '/\\$/N; s/\\\n//; ta' See Peter Krumins' Famous Sed One-Liners Explained, Part I, 39. Append a line to the next if it ends with a backslash "\".


6

I'd use an XML handling tool like xsh: open subtitles.xml ; for /transcript/Item { echo position() ; echo @from '-->' (@from + @duration) ; echo text() ; } Output: 1 1.16 --> 5.84 (Dong-hyuk is coming to see you now.) 2 5.92 --> 6.92 It's cold. 3 9.04 --> 11.92 - Hello. - Hello. 4 12.2 --> 13.96 You're busy as always. 5 ...


6

If you want to sort/test for uniqueness the first field specifically, and your system has the GNU coreutils version of sort, then I think you could just use sort -nu file viz. $ sort -nu file 123 some text 334 some other text 341 more text From info coreutils 'sort invocation' The commands sort -u and sort | uniq are equivalent, but this ...


6

You seem to be hoping that (NF","$2 -1) will be treated as a function that will return the number of comma-delimited elements in field $2 - it won't. NF is always the number of fields in the record. Instead, you can use awk's split function split($2,a,",") which splits field $2 into an array a and returns the number of elements. You can also tidy up the ...


6

With grep, you can do: $ grep -vwF -f toremove.txt users.txt username, userid, sidebar_side, sidebar_colour "John Lennon", 90123412, "left", "blue" "George Harrison", 72349482, "left", "green" With awk: $ awk -F'[ ,]' 'FNR==NR{a[$1];next} !($4 in a)' toremove.txt users.txt username, userid, sidebar_side, sidebar_colour "John Lennon", 90123412, "left", ...


6

Sed can't do arithmetic¹. Use awk instead. awk ' $4 == "calc" {sub(/calc( |\t)/, sprintf("%-6.2f", $3 - $2))} 1' The 1 at the end means to print everything (after any preceding transformation). Instead of the text substitution with sub, you could assign to $4, but doing so replaces inter-column space (which can be any sequence of spaces and tabs) ...


5

As a starter, why not sort on the xmlRecord Id column? :- sort -k 9 -o <out.log> <in.log> That should group them together in numerical order. If you want to sort by Task too, then it will probably involve an awk script or similar. How this works The sort command can sort data in a variety of ways, in this case we're sorting it based on the ...


5

This should work: printf "one\ntwo\n" | awk 'NR>1{print PREV} {PREV=$0} END{printf("%s",$0)}' ; echo " done" The script always prints previous line instead of current, and the last line is treated differently. What it does in more detail: NR>1{print PREV} Print previous line (except the first time). {PREV=$0} Stores current line in PREV variable. ...


5

You're using double quotes to delimit the string as well as inside the string itself, so the quoted string stops early and your internal quote characters aren't included: sed -i "s|"jdbc:mysql:... Ends here-^ You can escape each of the quotes inside the string: sed -i "s|\"jdbc:mysql://localhost/bajaj\",\"root\", ...


5

Sort works on a per-line basis, not on fields within a line. By default, it sorts based on the first character on the line and goes on from there. But you can also sort on "keys" other than at the beginning. This is useful when you want to sort on last name, or a numeric value at the end of the line, or so on. That's what the -t flag is for — it won't break ...


5

If your system has the GNU version of sed, you can use the GNU extension r command: r filename As a GNU extension, this command accepts two addresses. Queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, it ...


5

You can use cut: cut -d: -f1,4 file Linda:18 Steve:33 Henry:19 Alice:32 Robert:21 Olga:19 Kimberly:25 Henry:26 Carol:23 Thomas:32


4

Use external unix command nl. :'<,'>!nl -w 3 -n rz -s' '


4

Try this: $ awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' file2 file1


4

sort operates on entire lines. By default, it sorts on the entire contents of that line, but -k can be used to sort on one or more fields within those lines. -t can be used to change the delimiter between fields. I can't think of a case where using -t without also using -k makes any sense. Your second command, which is equivalent to: printf "%s\n%s\n" ...


4

If we can safely assume that you want to delete all lines that start with Products (including the space after the 1st word), these will all work: awk awk '$1!="Products" file > newfile perl perl -ne 'print unless /^Products/' file > newfile or perl -ane 'print if $F[0]!="Products"' file > newfile or, to edit the file in place perl -i -ne ...


4

With grep grep -Ff File2 File1 With awk awk 'NR==FNR {a[$1]++;next} a[$1]' File2 File1


4

The best strategy would be to use a proper html parser that can spit out the value of all <a> tags. Here, xmlstarlet is specifically an XML parser, and your HTML may not be well-formed XML, but you might get the idea: echo '<html> <a href="000000.jpg" title="image name.jpg" target="_blank">Image name.jpg</a> </html>' | ...


4

If you're using GNU sed (which bare -i suggests you are), there is a "word boundary" escape \b: sed -i "s/\b$SEARCH\b/$REPLACE/g" \b matches exactly on a word boundary: the character to one side is a "word" character, and the character to the other is not. It is a zero-width match, so you don't need to use capturing subgroups to keep the value with \1 and ...


4

I suppose the URI ends with a space: grep -o 'magnet://[^ ]*' filename Update: grep -o "magnet:?xt[^']*" filename Update: cat * | grep -o "magnet:?xt[^']*" or grep -oh "magnet:?xt[^']*" *


4

To copy all lines between %packages and %end from file1 into file2: awk '$1=="%end" {f=0;next} f{print;next} $1=="%packages" {f=1}' file1 >>file2 This solution is designed to remove the lines %packages and %end. (If you want those lines to be transferred as well, there is an even simpler solution below.) Since awk implicitly loops over all lines ...


4

You can use awk: $ awk -F':' '{print $1,$4}' file Linda 18 Steve 33 Henry 19 Alice 32 Robert 21 Olga 19 Kimberly 25 Henry 26 Carol 23 Thomas 32


4

If the existing commented lines form a single contiguous block, then you could match from the first commented line instead, commenting-out only those lines up to and including your end pattern that are not already commented sed '/^#/,/dotan/ s/^[^#]/#&/' file If the existing comments are not contiguous, then due to the greedy nature of the sed range ...


4

Nunber of lines before and after a match, including the match (i.e. you need to subtract 1 from the result if you want to exclude the match): sed -n '0,/pattern/p' file | wc -l sed -n '/pattern/,$p' file | wc -l But this has nothing to do with IP addresses in particular.


3

Try this awk: awk '!($1 in a){a[$1];print}' file 123 some text 334 some other text 341 more text This assumes that file was sorted.



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