Tag Info

Hot answers tagged

17

You can do it with single command with sed 's/\(.*\)-/\1 /' The point is that sed is very greedy, so matches as many characters before - as possible, including other -. $ echo 'swp-RedHat-Linux-OS-5.5.0.0-03' | sed 's/\(.*\)-/\1 /' swp-RedHat-Linux-OS-5.5.0.0 03


10

You could also handle this with bash parameter expansion: s=swp-RedHat-Linux-OS-5.5.0.0-03 echo ${s%-*} ${s##*-} Output: swp-RedHat-Linux-OS-5.5.0.0 03


8

This sounds like a task for join: join -t":" -o "1.1,1.2,1.3,1.4,1.5,2.1,2.2,2.3" \ -j 2 <(sort -k2,2 -t: test1) <(sort -k2,2 -t: test2) Output: Julian:Brude:Other:Other:Other:Jb:Brude:kemin Robert:Dillain:Other:Other:Other:R:Dillain:bodent Megan:Flikk:Other:Other:Other:Mb:Flikk:kentin Jesus:Kimmel:Other:Other:Other:Jbb:Kimmel:verlin ...


7

Something like this worked for me, although I'm sure there are better ways echo "swp-RedHat-Linux-OS-5.5.0.0-03" | rev | sed 's/-/ /' | rev swp-RedHat-Linux-OS-5.5.0.0 03


6

If you are not insisting on sed, the best thing to do this would be lynx. lynx --dump <filename>.html This will output the content of the html file in the format the html code was intending to display. The only condition is that the filename should have a .html or .htm extension.


5

You can combine multiple translations (excepting complex cases involving overlapping locale-dependent sets), but you can't combine deletion with translation. <doyle_sherlock_holmes.txt tr -d '[:punct:]' | tr '[:upper:] ' '[:lower:]\n' Two calls to tr are likely to be faster than a single call to more complex tools, but this is very dependent on the ...


5

That's not a sed command inside sed, that's just a replacement that your shell makes before actually caling sed. Just use proper quoting and parentheses, and you're done: sed -i.bak -e "$(sed -n 1,1p file.txt),\$d" output.file Double quotes allow process substitution inside (single quotes don't), and $() syntax means that the result of the inner command ...


5

This is simple task for awk: awk -F':' -vOFS=':' 'NR==FNR{a[$2]=$0;next}{print $0,a[$2]}' file2 file1 First we set : as field separator both for input (with -F) and output (with OFS) then if first file is processed (file2) we assign whole line to table element indexed with second field. When next next file (file1) is processed we print its lines adding ...


5

You can use another tool that lets you set the input record separator. For example Perl perl -pe 'BEGIN{ $/="}{" } s/}{/}\n{/g' file The special variable $/ is the input record separator. Setting it to }{ defines lines as ending in }{. That way you can achieve what you want without reading the entire thing into memory. mawk or gawk awk -v RS="}{" ...


4

$ sed '/a/ r b.txt' a.txt a d e f b c The condition /a/ looks for a line containing a. On such a line, the command r b.txt is executed which reads the file b.txt.


4

Here are a few approaches: GNU grep and tr: find all words and make them lower case grep -Po '\w+' file | tr '[A-Z]' '[a-z]' GNU grep and perl: as above but perl handles the conversion to lower case grep -Po '\w+' file | perl -lne 'print lc()' perl: find all alphabetic characters and print them in lower case (thanks @steeldriver): perl -lne 'print lc ...


4

You need to write your regular expression in a way that only matches whole words. With GNU sed, you can use \b which matches at word boundaries: sed -i "s/\b$word\b/$replace/g" If you know there will always be a space there, you could just add a space: sed -i "s/ $word /$replace/g" Now, there are also some issues with your script. Your if ... break ...


4

$ awk '{split($1,a,"|"); split($2,b,"|"); for (i in a) {for (j in b) print a[i],b[j];}}' file TMPRSS2 AADAT pp9284 AADAT ERG TMPRSS2 ERG pp9284 TMPRSS2 ETV1 pp9284 ETV1 PDE4A RAB4B-EGLN2 PDE4A EGLN2 PDE4A MIA PDE4A MIA-RAB4B PDE4A RAB4B To also print the third (SampleN) field, you can just add $3 to the print statement inside the loops, i.e. $ awk ...


4

With bash: # important to use parentheses, not braces, to localize changes to IFS # the variable is purposefully unquoted split_pipe() ( IFS='|'; echo $1 ) while read -r first second third; do for word1 in $(split_pipe "$first"); do for word2 in $(split_pipe "$second"); do echo $word1 $word2 $third done done done < file


3

One way to classify some 'old' commands - though not for 'more powerful' - is that they are builtin commands, meaning a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute. Examples include logout, cd, echo and history. You can see if a ...


3

Try the following awk-script: awk ' /<Person>/,/<\/Person>/{ if (! /<Name>/) a=a s $0 s="\n" } /<\/Person>/{ system("echo \""a"\" |md5sum - ") a=s="" }' input.xml Explanation: Collect all lines between tags Person into a variable When meet closing tag /Person call system command than empty a ...


3

Try: for f in ./*.txt; do sed -i '' -e "s/$/$(printf '\t')$f/g" "$f" done


3

Perl to the rescue: perl -i~ -e ' $/ = \1024; while (<>) { print "\n" if $closing and /^{/; undef $closing; s/}{/}\n{/g; print; $closing = 1 if /}$/; } ' input1 input2 Setting $/ to \1024 will read the file in chunks of 1024 bytes. ...


3

With zsh: setopt extendedglob # best in ~/.zshrc extract_numbers() REPLY=${(SM)REPLY##<->-<->} for file (*<->-<->*.dat(no+extract_numbers)) { do-what-you-will-with $file } We define a sorting function (extract_numbers) that returns the Substring of the file name that Matches <->-<-> (<-> matching any positive ...


2

sed '/From/,$!d;/./!q' <infile ...the above expression instructs sed to delete from output all lines which do !not fall within the range of /From/ through the $last line. Each time it deletes a line it stops reading its script and starts over with the next input line from the top - and so it doesn't read the next command. So the /./!quit command means ...


2

You put quoting chars into a string. The problem is that quoting chars are not parsed after parameter expansion. In order to get the desired effect you would need eval sed ... $drill_file You can use set -x to see how the shell sees the command line: > text=a\\\ \\\ \\\ b > echo $text a\ \ \ b > set -x > : $text + : 'a\' '\' '\' b I.e. the ...


2

You don't need to do the first sed if you quote your variables: read -p "Give relative or absolute path to the \"drill.TXT\" file: " drill_file if [[ -f "$drill_file" ]]; then sed 's/\(^X[[:digit:]]*\)[[:digit:]]\(Y[[:digit:]]*\)[[:digit:]]/\1\2/' "$drill_file" echo "Conversion finished." else echo "no such file: '$drill_file'" fi


2

FWIW, GNU awk awk -vRS='\\n\\+?' '{ORS=RT == "\n+"? "":RT; print}' trail.txt data net06706 net06707 net221 net222 net223 m1 net10 rwl vdda vss nch l="l1pg+0.005u" w=w1pg+0.105u m0 vdda rwld net10 vss nch l="l1pg+0.005u" w=w1pg+0.105u


2

I don't think you can use a backspace approach - however you could do something like sed -e :a -e '$!N;s/\n+/ /;ta' -e 'P;D' trail.txt See Famous Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution, 40. Append a line to the previous if it starts with an equal sign "=" (with the obvious substitution of + for =).


2

Use in-line document with cat cat >destfile <<EOF <your ZML here...where $vas expand happly> but the rest remain as is EOF I used it in a lot of scripts


2

Frustratingly perhaps, one of the combinations you omitted would have worked: echo "<root><foo a=\"b\">$lorem</foo><bar value=\"ipsum\" /></root>" >> $MY_XML Single quotes leave everything exactly as you type it. Escaping with \ will not work. Double quotes will evaluate the string. Inside double quotes you can use \ to ...


2

Print the line on this way: echo '<root><foo a="b">'"$lorem"'</foo><bar value="ipsum" /></root>' >> "$MY_XML" This is need because single quotes deny shell interpreter from replace environment variables inside


2

Awk will be the best for your scenario. $ awk -F'"' '{gsub(",", "", $2);print}' file.txt 0,1,,, 10815197 , 6,7,010202,, 5589 , 6,7,010202,,589, How it works -F'"' - causes AWK to use double quotes ( " ) as record separator. gsub(",","",$2) - gsub function will search and replace all occurrence of double quotes with empty string. print ...


2

Try this (Chris, you're very close): sed -i '' 's|export TODO_FILE="$TODO_DIR/todo.txt"|export TODO_FILE="$TODO_DIR/writing.txt"|g' ~/.todo/config


2

Yes. You can do that w/ tr in an ASCII locale (which is, for a GNU tr anyway, kind of its only purview). You can use the POSIX classes, or you can reference the byte values of each character by octal number. You can split their transformations across ranges, as well. LC_ALL=C tr '[:upper:]\0-\101\133-140\173-\377' '[:lower:][\n*]' <input The above ...



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