Swap two columns – awk, sed, python, perl
You can do this by swapping values of the first two fields: awk ‘ { t = $1; $1 = $2; $2 = t; print; } ‘ input_file
You can do this by swapping values of the first two fields: awk ‘ { t = $1; $1 = $2; $2 = t; print; } ‘ input_file
You should use \(.*\) instead of (.*). sed uses Basic Regular Expressions (BRE) by default, which uses \( and \) for group capturing, not just ( and ) as used in Extended Regular Expressions (ERE). Since your expression used (.*) instead of \(.*\), it is not recognised as a group capture, and thus nothing is … Read more
This might work for you: sed ‘/text\|blah/!d’ file some text here blah blah 123 some other text as well
Try this: find /home/user/ -type f | xargs sed -i ‘s/a\.example\.com/b.example.com/g’ In case you want to ignore dot directories find . \( ! -regex ‘.*/\..*’ \) -type f | xargs sed -i ‘s/a\.example\.com/b.example.com/g’ Edit: escaped dots in search expression
If you don’t want to print lines that don’t match, you can use the combination of -n option which tells sed not to print p flag which tells sed to print what is matched This gives: sed -n ‘s/…/…/p’
Quote sed codes with double quotes: $ sed “s/ones/one’s/”<<<“ones thing” one’s thing I don’t like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way: $ sed ‘s/ones/one\x27s/'<<<“ones thing” one’s thing
You can use the in place option -i of sed for Linux and Unix: sed -i ‘s/[ \t]*$//’ “$1” Be aware the expression will delete trailing t‘s on OSX (you can use gsed to avoid this problem). It may delete them on BSD too. If you don’t have gsed, here is the correct (but hard-to-read) … Read more
You could try using something like: sed -n ‘s/$/:80/’ ips.txt > new-ips.txt Provided that your file format is just as you have described in your question. The s/// substitution command matches (finds) the end of each line in your file (using the $ character) and then appends (replaces) the :80 to the end of each … Read more
The easiest way would be to use a different delimiter in your search/replace lines, e.g.: s:?page=one&:pageone:g You can use any character as a delimiter that’s not part of either string. Or, you could escape it with a backslash: s/\//foo/ Which would replace / with foo. You’d want to use the escaped backslash in cases where … Read more
sed -i ‘8i This is Line 8’ FILE inserts at line 8 This is Line 8 into file FILE -i does the modification directly to file FILE, no output to stdout, as mentioned in the comments by glenn jackman.