sed

sed: conditional merge of multiple lines

与世无争的帅哥 提交于 2021-02-06 12:51:11
问题 I need to merge line of a file using sed based on a pattern. eg: Input File: X1 A B C D E F \+ G H I J 1 \+ LK T PP E OO 2 X2 DDF F Y \+ J W Q .... OutPut Expected: X1 A B C D E F G H I J 1 LK T PP E OO 2 X2 DDF F Y J W Q .. I would like to equivalent of wat is possible in vi editor (:%s/\n+/ /g) Searching the web I found a solution, which logically should have worked sed -e '{:a; N; s/\n+/ /g; ta}' infile But this command defies my understanding and logic, and has produced output X1 A B C D

Sed \w character class not working with replace on mac

拟墨画扇 提交于 2021-02-05 12:28:46
问题 When I execute this command on OSX, I would expect each letter to be replaced by an "x", but none of the letters are replaced: $ echo 'a b c' | sed 's/\w/x/g' output: a b c This works just fine: echo 'a b c' | sed 's/[[:alpha:]]/x/g' output: x x x What am I missing? 回答1: Please note that the BSD implementation of sed on Mac is different from other linux machines You can install gnu sed. Run the following command on Apple Mac OS: $ brew install gnu-sed reference: https://www.cyberciti.biz/faq

adding && \ to each line on a text file except last line

点点圈 提交于 2021-02-05 09:11:46
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

adding && \ to each line on a text file except last line

北城余情 提交于 2021-02-05 09:11:38
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

adding && \ to each line on a text file except last line

余生颓废 提交于 2021-02-05 09:11:01
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

adding && \ to each line on a text file except last line

倖福魔咒の 提交于 2021-02-05 09:07:52
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

Trimming a part of file extension for all the files in directory - Linux

巧了我就是萌 提交于 2021-02-05 08:45:15
问题 I have a requirement in which I want to trim the file extension for all the files contained in a directory: - The file will be like; America.gz:2170 Europe.gz:2172 Africa.gz:2170 Asia.gz:2172 what I need is to trim the :2170 and :2172 from all the files, so that only the .gz extension remains. I know that with the help of below SED code, it is possible for all the entries in a file, however I need for all the files in a directory: - **sed 's/:.*//' file** Any bash or awk code to fix this will

Renumbering duplicate lines with counter awk

跟風遠走 提交于 2021-02-05 08:38:08
问题 I have duplicate words in csv. And i need to count it in such way: jsmith jsmith kgonzales shouston dgenesy kgonzales jsmith to this: jsmith@email.com jsmith1@email.com kgonzales@email.com shouston@email.com dgenesy@email.com kgonzales1@email.com jsmith2@email.com I have smth like that, but it doesn't work properly for me..or i cant do it enter link description here 回答1: A simple way to do it is maintain an array using the username as the index and increment it each time you read a user, e.g.

How to search and replace the specific line number string

落爺英雄遲暮 提交于 2021-02-05 08:17:45
问题 line_index="2d"; file="./Desktop/books.sh"; sed -i.bak -e $line_index $file Will delete the entire line that $line_index is pointing to sed -i "s/harry/potter/g" $file will search for harry and replace with potter Is there anyway that i can combine both statement so it will only replace the specific line number of harry instead of all the harry in the file. 回答1: Sure, "addresses" and "commands" can be freely combined: sed '2s/harry/potter/g' 回答2: To restrict substitution to a line number, add

How to substitute words in Git history & properly debug related problems?

£可爱£侵袭症+ 提交于 2021-02-04 19:24:05
问题 I'm trying to remove sensitive data like passwords from my Git history. Instead of deleting whole files I just want to substitute the passwords with removedSensitiveInfo . This is what I came up with after browsing through numerous StackOverflow topics and other sites. git filter-branch --tree-filter "find . -type f -exec sed -Ei '' -e 's/(aSecretPassword1|aSecretPassword2|aSecretPassword3)/removedSensitiveInfo/g' {} \;" When I run this command it seems to be rewriting the history (it shows