Tag Info

New answers tagged

2

Here's a solution with ed: ed -s filename <<< $'g/PATTERN/?{?,/}/p\nq\n' that is: g/PATTERN/ # mark each line matching PATTERN ?{?,/}/p # for each marked line, print all lines from the previous { up to the next } q # quit editor This assumes there's only one line with PATTERN between each pair of { } otherwise you ...


0

Here are two commands. If you want a command that trims up to the last .*{$ line in a sequence (as @don_crissti does with ed) you can do: sed 'H;/{$/h;/^}/x;/{\n.*PATTERN/!d' ...which works by appending every line to Hold space following a \newline character, overwriting hold space for every line that matches {$, and swapping ing hold and pattern spaces ...


-1

with awk: awk '/PATTERN/{print $0 RS}' RS='}' file if record matched with PATTERN in it then print that record$0 and then print the } ( RS prints }) or using sed command I found it here sed -e '/./{H;$!d;};x;/PATTERN/!d' file


-2

sed -n '/string/p' filename the -n when added to sed suppressed sed's default behavior this statement might not give you exactly what you want but it should just displace the string i hope this helped


0

A slight variation on @jillagre's answer (modified for robustness) could look like: sed 's/p\(attern\)/p\n\1/;s///g;s/\n//' ...but in some seds you may need to replace the n in the right-hand side of the first s///ubstitution statement with a literal \newline character.


2

It's not too hard with sed really. You can always delimit a section with a \newline or you can trade out a delimiter for a \newline temporarily. And you can do it without a loop: sed 's/$/START/;s/END/& /g; y/D\n/\nD/ s/\([^D]*START\)*[D\"]*/\1/g y/\n/D/;s/.....$// ' <<\IN \"XXX \ START sapiodj ...


1

The following just uses nl mainly, though tr gens a bunch of blank lines and sed prepends the REQ bit... IFS=\ read -r l r <file tr \\0 \\n </dev/zero| nl -ba -w4 -nrz -s" $r"| sed "s/^/${l%Q*}Q/;4096q" First it saves the two halves in $l and $r with read. Then it translates 4096 nuls into \newlines - which nl counts and appends $r to, and last ...


0

This prints the whole line by substituting a new value for field 2 and should work regardless of how many fields may appear later in the line. awk -F 'REQ| ' '$1 == "(REF-" {field1 = $1; $1 = ""; num = $2; for (i = 0; i < 4096; i++) {$2 = sprintf("%sREQ%04d", field1, num + i); line = $0; sub("^ ", "", line); print line}; next} {print}' inputfile Broken ...


3

perl would be good for this: perl -ne 'for $n (1..4096) {s/\d+/ sprintf "%04d", $n /e; print}' File > File2


1

I'm not sure if BOF and EOF are parts of the file, but anyhow you can try the following awk code: awk 'BEGIN{FS="REQ| "}/[[:digit:]]/{for(i=0;i<4096;i++){printf($1"REQ%04d "$3"\n",$2+i)};next}1'


1

With recent versions of GNU sed: sed -E '/^AUTH:/!b;s/([^\w:])(\w+)/\1\L\u\2/g' With older versions: sed -r '/^AUTH:/!b;s/([^[:alnum:]:])([[:alnum:]]+)/\1\L\u\2/g'


2

Assuming you don't have ` character in the file. If you do just change in the line bellow all ` to anything else. sed -e 's/END/`/g;:X' -e 's/\(START[^`]*\)["\]/\1/g;tX' -e 's/`/END/g'


1

With sed: sed 's/:/::/g;s/</:l/g;s/>/:g/g; # escape :, <, > s/START/&</g; s/END/>&/g; # replace START/END with <> :1 s/\(<[^>]*\)[\"]/\1/g;t1 s/[<>]//g;s/:g/>/g;s/:l/</g;s/::/:/g; # restore <>:' With perl: perl -pe's|START.*?END|$&=~y/\\"//rd|ge'


0

You have indicated in a comment that awk is also allowed. So I'm basing my answer on this. Assuming your STARTs and ENDs are balanced, if you split the line on either word, you find that you want to remove backslashes and double quotes from all even-numbered fields. To this end: awk -F 'START|END' '{ for(i=2;i<=NF;i+=2){ # For each ...


2

In awk: awk -F '' -vOFS='\t' '{$1=$1}1' file > new_file Borrowed the idiom {$1=$1}1 from an answer to one of your other questions. This sets the field separator to nothing (-F ''), which means that each record is read character-by-character. The output field separator is set to a TAB character (-vOFS='\t') and the idiom {$1=$1}1 is (as far as I can ...


0

I think you should give fold a go: tr \\n \\r <infile | fold -w1 | tr '\r\n' '\n\t' >outfile I preprocess fold's input w/ tr by replacing the instream \newline characters w/ \returns. fold is printing a \newline character for every column in input - each of your capital letters - but it resets its counter on \returns and so when tr does the final ...


2

sed -e '/^AUTH:\([^[:alpha:]]*\)/!b' -e 'h;s//\1/;x;s/// s/\([[:alpha:]]\)\([[:alpha:]]*[^[:alpha:]]*\)/\1/g;x;s//\ \2/g y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;G;:l s/\n\(.*\n\)\(.\)/\2\1/;tl s/\(.*\)\n/AUTH:\1/ '<<\IN TITLE: Average title AUTH: SUPERMAN ...


5

If I have understood your intention correctly then this does it: sed -e 's/./&\t/g' -e $'s/\t$//' file The second replacement deletes the tab at the end of the line.


2

Try doing this using perl: perl -ne 'print join "\t", split //' file > new_file


3

> sed '/^AUTH/{s/^AUTH: //;s/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g;s/^/AUTH: /;}' file TITLE: Average title AUTH: Superman AFF: Something AUTH: The New One AFF: Berlin AUTH: Mars-Mensch AFF: Planet Mars


5

Depending on the version of sed on your system you may be able to do sed -i 's/Some/any/; s/item/stuff/' file You don't need the g after the final slash in the s command here, since you're only doing one replacement per line. Alternatively: sed -i -e 's/Some/any/' -e 's/item/stuff/' file The -i option tells sed to edit files in place; if there are ...


6

You can chain sed expressions together with ";" %sed -i 's/Some/any/g;s/item/stuff/g' file1 %cat file1 anything 123 stuff1 anything 456 stuff2 anything 768 stuff3 anything 353 stuff4


5

Multiple expression using multiple -e options: sed -i.bk -e 's/Some/any/g' -e 's/item/stuff/g' file or you can use just one: sed -i.bk -e 's/Some/any/g;s/item/stuff/g' file You should give an extension for backup file, since when some implementation of sed, like OSX sed does not work with empty extension (You must use sed -i '' to override the original ...


0

If you have GNU grep, you can use a perl-compatible regular expressions. This is handy due to the look-around assertions: grep -oP '(?<=track_name).*?(?=,)' filename


1

This is an example regex that matches your provided input and gives you the output you desire. Using this method, we're causing sed to do a search+replace (s) on each line, replacing the whole line with just the part in the middle, if the line contains track_name and a comma. We then only print matched lines (p). [me:~]$ cat work/tmp/example.txt ...


0

With GNU sed: sed 's/\([[:digit:]]\+\)\(\.[[:digit:]]*\)\{0,1\}-\(\W\)/-\1\2\3/g' GNU version of sed is needed in order to match "non-word" character after - with \W. You can simulate this with non-gnu version using [^a-zA-Z0-9_]. If - should not be changed also in case it precedes space than just add space to previous pattern, so: sed ...


4

You're mixing character classes (a list of characters inside square brackets) with the smb.conf share names which are surrounded by square bracket literals. Also, the echo command is not well-formed: in the case where sed exits with a non-zero status, the shell will attempt to invoke the command Failed. A few suggestions: Remove the character class (outer ...


2

Better use an XML parser with a XPath expression to modify the XML file. One example with xmlstarlet : $ xmlstarlet edit -L -u "/connection/output[contains(., "Arturia")]" -v "remplacement_string" file.xml


2

The command you have above will (somewhat clumsily) rename all files in the current directly from *.jpg to *.jpeg, it could be modified to delete all files but is hardly appropriate to the task. However, it sounds like you are trying to craft a suitable filename such that when the above command encounters it, it will delete everything in the current ...


3

There's no need to use sed, this can be handled by parameter expansion mv -- "$x" "${x//%20/ }" FWIW, I'd be replacing those %20s with an underscore (or something); I hate file names that contain spaces. But I guess learning how to write bash scripts that can handle spaces and other special characters in file names is a Good Thing. :) As Izkata mentions ...


5

Use rename and replace the %20 with a space in all type of files: $ rename -n 's/%20/ /g' * File%20with%20in00.yA2 renamed as File with in00.yA2 File%20with%20in01.h9H renamed as File with in01.h9H File%20with%20in02.CNR renamed as File with in02.CNR File%20with%20in03.PuP renamed as File with in03.PuP File%20with%20in04.js8 renamed as File with in04.js8 ...


6

Since the output of the mv probably will have spaces you need to put double quotes around the result in order not to try and execute commands in the for loop like: mv abc%20def abc def where mv has too many arguments. These are the ones giving you the usage: message. What you should do is: for x in *_MG*.CR2 do mv -- "$x" "$(printf '%s\n' "$x" | sed ...


3

Depending on the size of the file you can do: sed '/^*/!H;//p;$!d;g;s/\n//' That stacks in Hold space lines which do not match /^*/. Those that do match are printed as they occur in input. Then all lines which are !not the $last are deleted from output. On the last line we get hold space by overwriting pattern space, then the first \newline character is ...


1

Here is a solution using ed (and expanding a bit on the vi solution posted by Kaz, which underneath uses ex - a beefed up ed). Replace w with ,p\n to print the output instead of writing to file. Move the matching lines to the end of the file: ed -s file <<< $'g/^\*/m$\nwq\n' The global subcommand marks every line that matches the pattern. ...


1

As a variation on the answer already here - it is easier still, probably, to pull the script directly out of a pipe: sed 's|.*|s/&//g|' <patterns | sef -f - infile >outfile That way you needn't alter the patterns file - or any other - directly and can instead modify it in-stream to suit your needs.


1

Others have provided general answers for your question which demonstrate good ways of parsing json however I, like you, were looking for a way to extract an aws instance id using a core tool like awk or sed without depending on other packages. To accomplish this you can pass the "--output=text" argument to your aws command which will give you an awk parsable ...


2

If you previous sort your files, you could do in a simple way: $ join file2 file1 ENSG00000223116 0 0 AL157931.1 ENSG00000233440 1.71449394 50 HMGA1P6 To sort your files just do the following: sort file1.txt > file1 sort file2.txt > file2


1

You could get the information from the source (on Linux): awk '/^Cached:/{print $2}' /proc/meminfo This does get the value in kB (for me), so you could convert it for MB: awk '/^Cached:/{print $2/1024}' /proc/meminfo


2

Try doing this for memory cached value : LANG=C /usr/bin/free -m | awk 'NR==2{print $7}'


2

sed 's/^.*/s\/&\/\//' pattern > sed-pattern-file sed -f sed-pattern-file myinitialfile.txt > mycleanfile.txt


2

I would use the following: sed -r '/(SEM|AFF|CON)/ s/([:,] *)[Tt]he */\1/g' file Add -i option to change file in place.


1

Try this way: egrep -rl "^(SEM|CON|AFF)\: (t|T)he" * | xargs sed -r -i 's/(^(SEM|CON|AFF):\s)((t|T)he[ ]*)/\1/g'


2

Just use one sed expression (needs GNU sed): sed -r -i -e '/(SEM|AFF|CON)/s/([:,]\s*)the\s+/\1/ig' * The search pattern at the begin of the sed command restricts the replacement to the lines which begins with the selected categories. The i flag for the replace command (s//) makes the pattern case-insensitive, the g flag allows more than on replacement in ...


4

You could use perl (get the file content and substitute pattern with pattern+file content): perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/' file.txt add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.: perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/g' file.txt Another way, using ed: ...


2

This feels like it could be done in a simpler way, but the best I can come up with after an hour of head-scratching is this python script: #! /usr/bin/env python3 import sys, os class Block: block_id = '' source1 = '' source2 = '' mixtures = [] def __init__(self, block_id = '', source1 = '', source2 = '', mixtures = []): ...


2

You're on the right track with sed: all you need to do is to transform your list of line numbers to be followed by a p and a newline, and use that as a sed script. For example, if you have a space-separated list: lines="2 3 5 7 11 13" <sourcefile.tsv sed -n "$(echo "$lines" | sed 's/$/p/; s/ /p\n/')" >extractedrecords.tsv Awk is another ...


3

Should be simple enough in sed: sed 's/.ENSG[0-9]*$//'


4

So it would be a little tricky to make this work portably in sed - you should be looking to cut and/or paste with some regex precursor generating their script in that context - and this is because sed will always insert a \newline before the output of a read. Still, w/ GNU sed: sed '/First/{x;s/.*/cat file/e;H;x;s/\n//}' <<\IN First Second Third IN ...


4

In your linked question there is already good awk answer, just modify it a little bit by using printf instead of print to insert the content without newline: awk '/First/ { printf $0; getline < "File1.txt" }1' infile.txt Result: Some Text here FirstThis is text to be inserted into the File. Second Some Text here You may want to add space or other ...


2

Assuming linenumbers.txt has one number per line awk 'NR == FNR{a[$0]; next};FNR in a' linenumbers.txt sourcefile.csv > extractedrecords.tsv Might do the job. Or, with bash join -t':' -o2.1,2.2 <(sort linenumbers.txt) <(awk '{print NR":"$0}' \ sourcefile.csv | sort -k1,1 -t':') | sort -k1,1n -t':' | cut -f2- -d':' All the extra jumping ...



Top 50 recent answers are included