New answers tagged

0

With (GNU) sed (as per the tag in the question): $ sed -i -e '$a\</pre>' file.in This will add a line with </pre> on it to file.in. Alternatively: $ sed -i -e '$s@\(.*\)@\1</pre>@' file.in This will add </pre> to the last line in file.in.


1

You can also use what is known as a compound command. That's a list of commands, which the shell treats as a single command for the purpose of anything external to the compound command. Yes, that's somewhat of a recursive definition; an example makes it easier to understand. For example, to surround a file with <pre> and </pre>, you might use a ...


0

How about this: echo "</pre>" >> /path/to/the/file Or at the beginning is a bit trickier, you'd need to use a different file for the output. echo "<pre>" | cat - /path/to/the/file > /path/to/the/newfile cat means concatenate, and it has two arguments in this case: the - means stdin, so the first part of the concatenated file is ...


0

You could use sed like this: sed -n '/^2 /s/.*(\([^)]\+\)).*/\1/p' file.txt Or awk like this: awk -F "[()]" '/^2 / {print $2}' file.txt The first solution replaces the line with the string enclosed in parentheses before printing it. The second solution uses parentheses as field separators and then only prints field two (the enclosed string).


0

You could do it in awk. Put the following script in a file and then call awk -v pattern=disk60 -f script_file data_file: found { buf = buf "\n" $0 } /multipath *\{/ { buf = $0; found = 1 } $0 ~ pattern { matched = 1 } /\}/ { if (matched) { gsub(/\n/, "\n#", buf); buf = "#" buf; } print buf "\n"; found = matched = 0; } This ...


0

Finally figured out why this wasn't working - what I thought were hyphens weren't actually hyphens, they were some weird Unicode character than looks nearly exactly like a hyphen! So my script wasn't picking up on it. Ultimately the script I wrote the first time around works like a charm: #!/bin/bash i=1 for f in *.mp3 do if [[ $i -lt 10 ]] ; then ...


0

You could also use sed: sed -r '400,600s/(.{21}).(.*)/\1B\2/' file.txt


1

You're doing a relational "join" operation, so use the standard UNIX join command: $ join fileA.txt fileB.txt 4 text3 5 text4 8 text7 9 text8 To only get the second field from the second file, add -o 2.2: $ join -o 2.2 fileA.txt fileB.txt text3 text4 text7 text8 Both files needs to be sorted on the join field (the first column in this example) for this ...


1

$ awk 'FNR==NR{seen[$1];next;} FNR in seen' FileA FileB text3 text4 text7 text8 How it works FNR==NR{seen[$1];next;} While reading the first file, this adds each number as a keey to the associative array seen. FNR is the line number of the current file and NR is the line number among all lines read. So when FNR==NR, then we are still reading the first ...


1

You can create a simple sed script from the file A by appending p to each line, then run the script in another sed with -n not to print the other lines. Moreover, there's no need to save the generated script, you can pipe it between the seds: sed 's/$/p/' A | sed -nf- B



Top 50 recent answers are included