Tag Info

New answers tagged

4

Re the command you have provided: grep "5628" test.csv | sed 's/,*$//g' This will output lines matching '5628' with all trailing commas removed. It will not update the file test.csv. However, you indicated the file came from a Windows machine, so the line endings are CR/NL instead of just NL. The result is that there is a hidden CR at the end of the ...


2

Another answer, you could use vi/vim! qdjddq And then if your file was 500 lines (for example) type 250@d And then to write and exit type :x Or if something goes wrong and you don't want to save: :q! Explanation: q #Start Recording d #Put the recording into register 'd' j #Move the cursor down dd #Delete the line ...


1

Another option (shorter) sed 'n; d' file


47

Solving this by deleting every second line can be error prone (for example, when process sometimes generates two meaningful lines instead one). May be it is better to filter out the garbage: grep -v "No error occurred" file It can run as filter, you can add more garbage patterns here and improve the result.


4

Here is a way using sed: sed -n 'p;n' filename Another way with GNU sed: sed -n '1~2p' filename Output from above commands: Data inserted into table. Total count 13 Data inserted into table. Total count 45 Data inserted into table. Total count 14 Data inserted into table. Total count 90


9

Ассоrding to the question, with GNU sed: sed '0~2d' file will delete every second line but I'd like to offer filter lines by it content: sed '/Data/! d' file or with same result sed '/No error/d' file


18

With sed: sed -e n\;d <file With POSIX awk: awk 'FNR%2' <file If you have older awk (like oawk), you need: oawk 'NR%2 == 1' <file With ex: $ ex file <<\EX :g/$/+d :wq! EX will edit the file in-place. g mark a global command /$/ match every lines +d delete the next line wq! save all changes This approach share the same ideal ...


3

You can try with awk: awk 'NR % 2 != 0' file or you can print only lines containing Data inserted: awk '$0 ~ /Data inserted/' file


0

Try this, it will help you. Open the file in vi editor: $ vi /file/location :1,100 s/^/DMS/ --> Enter 1,100 is exactly from which line you want to start and end. s is the substitution command and ^ means the the line starting. Or you can do it without opening the file, using GNU sed: sed -i 's/^/DMS /' /your/file/location


2

With sed: sed '/^$/{$!{N;/\n$/D;s/.//;$!h;$p;d};};//!{H;1h;$!d};$x' infile this should print the last set of non-empty lines without any leading/trailing empty lines. e.g. iostat -d 1 2 | sed '/^$/{ # if the line is empty $!{ # and if it's not the last line N # then pull in the next line ...


1

You don't have to cut by -character count. Instead, you can cut on a specified -delimiter. grep '^<gc type="global"' <infile | cut -d\" -f4 80 81 82 That gets only the 4th "-delimited field on a line. And so that field can be as many characters - or digits if you prefer - as you like, so long as none of them are double-quotes. But you don't ...


1

If your paragraphs are always separated by a single empty line: sed '/^$/s/^/\x02/' infile | tr \\n$'\002' $'\003'\\n | \ sed 's/^\x03//;1s/\x03$//;1!G;h;$!d;$a\' | tr $'\003' \\n It's quite easy to see how it works if you break it into pieces and run sed '/^$/s/^/\x02/' infile then sed '/^$/s/^/\x02/' infile | tr \\n$'\002' $'\003'\\n and so on... If ...


0

You could grep to find the lines with 'type="global"' and then use sed to extract the values within the quotes after 'id=':' $ grep 'type="global"' /tmp/foo | sed -e 's/^.*id="\([0-9]*\)".*$/\1/' 80 81 82 ...


0

awk -F[=\"] '{ a[FNR]=$0; if ($6>l && $0 ~ "<gc type=\"global\""){ l=$6;f=1;s=FNR }; if($0 ~ "</gc>" && f==1 ){ e=FNR;f=0 }} END{ for (i=s;i<=e;i++) { print a[i] }}' This will print the block starts from <gc type="global" till </gc> that has largest id. All ...


3

The GNU sed solution needs -i for inline editing: sed -i 's/$/ 1/' file.txt (With BSD sed, you need sed -i '') In awk, you have to work around that with a temporary file: awk '{ print $0 " 1" }' file.txt > tmp && mv tmp file.txt In fact, sed also creates a temporary file, but -i handles that The Problem with awk '{ print $0 " 1" }' < ...


0

The following works: sed -e'/-/!N;/;\n/!b' <i >o \ -e's//:/;y/ /\n/;:n' \ -e's/^\(\([^:]*\).*\)\n/\1 \2:/;tn' Or, with -Extended regular expression syntax (which will work at least w/ AST/BSD/GNU seds): sed -Ee'/-/!N;/;\n/!b' <i >o \ -e's//:/;y/ /\n/;:n' \ -e's/^(([^:]*).*)\n/\1 \2:/;tn' ...which isn't terribly ...


0

Answer for First Version of This Question (Before the Data Changed) $ awk '/^[^;]*[[:alpha:]];/{a=$1; if (NR!=1)print"";getline; gsub(/(^| )/, " "substr(a,1,length(a)-1)":");print;next} {print " "$0;}' file MX04A:DMX04A; MX04A:DMX04A; MX04A:LMX04A; MX04A:LMX04A; -17.2; -15.3; -14.3; -13.6; -16.8; -15.4; -16.0; -15.3; LH36A:DLH36A; LH36A:DLH36A; ...


0

Here's a start, that at least works with the example data: sed -r '/[A-Z];/{N;s/([^;]+);\n([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)/\1:\2 \1:\3 \1:\4 \1:\5/;3,$s/^/\n/};s/^/ /' input.txt This is assuming the following: Line 1 of each record will always have some uppercase letters Line 2 of each record always directly follows line 1 Line 2 of each record always ...


1

I would think the quickest way to do it would look something like... sed -e's/./& /g;i\' -e'<item>' \ -ea\\ -e'<tag><out>=' <file | paste -d'\0 ""' - - - ./file /dev/null This also works: ( set -- - - - - - - /tmp/file paste -d'<item> <tag><out>="' "$@" - - - - - - "$@" | sed 's/ ...


1

You can do it without loop in awk: awk '{a=$1;gsub(/./,"& ",$1); print "<item>"$1"<tag><out>=""\""a"\""}' numbers.txt Output: <item>9 3 7 4 5 4 1 6 3 2 5 5 3 <tag><out>="9374541632553" <item>5 1 2 4 3 7 4 7 8 7 9 8 4 1 <tag><out>="51243747879841" <item>3 2 0 3 0 0 9 8 8 9 6 9 1 4 ...


1

Here's one way you might be able to do the whole thing in sed: sed ' h; s/./& /g; s/.*/<item>&<tag>out=/; G; s/\n\([0-9]*\)/"\1"/; ' numbers.txt


-1

How about this... cat numbers.txt | while read line; do echo $line | sed -r "s/([0-9])/\1/g; s/([0-9])/\1 /g ; s/^/<item>/g; s/$/<tag>out=\"$line\"/g" ; done Or, cat numbers.txt | while read line; do echo "<item>$(echo $line | fold -w1 | paste -s -d' ') <tag>out=\"$line\""; done


1

Because both read and sed are taking data from stdin. In the while loop, you read the first line into $line. Then sed starts: you don't give it any other input so it reads from stdin, which is the output of cat numbers.txt. So sed will consume the rest of the input. And since you're still in the first iteration of the while loop, the $line variable doesn't ...


1

From a user's perspective, a nice & simple Unix tool that does the job perfectly is qsubst. For example, % qsubst foo bar *.c *.h will replace foo with bar in all my C files. A nice feature is that qsubst will do a query-replace, i.e., it will show me each occurrence of foo and ask whether I want to replace it or not. [You can replace unconditionally ...


1

You just need n . dots. n=[num] sed "s/.\{$n\}keyword/REPLACE/" ...where [num] is meant to represent a positive integer should work. Note that is different than .*keyword in at least a couple of important respects. .* applies to any/all characters that occur before the last occurrence of keyword in sed's pattern space, and so if there are multiple ...


0

I did three versions of this. v1 sed -e'/^PATTERN_START/!b' -e:n -eN \ -e'/\nPATTERN_END$/!bn' -eh\;s/// \ -e'x;s/\n[[:print:]]*$//;x' \ -e's/\(\nRecord [[:print:]]*\)\{0,1\}\n/\1 /g' \ -e'G;P;D' data That one prints out the whole file after only applying edits to the Record lines which occur between ...


0

In this particular case - sorted input, so all con... words are listed before pro... words - you could use awk to store the lines matching ^con in an array and when you reach the lines matching ^pro, replace pro with con and print if the result is in array: awk '/^con/{arr[$0]=$0}; /^pro/{c=gensub(/pro/, "con", 1) if (c in arr) print c, $0}' ...


2

From my earlier comment to the question itself: egrep '^(pro|con).* /usr/share/dict/words | sed -nE 's/^(pro|con)(.*)/\2/p' | sort | uniq -d will give you a list of all the word-bases that have both a pro and con prefix: The initial egrep grabs all the words with pro and con prefixes. We then use sed to strip off pro and con from the beginning of each ...


0

This will print out the words without the pro|con prefix: grep '^\(pro\|con\)' /usr/share/dict/words | cut -c 4- | sort | uniq -c | awk '$1 == 2 {print $2}'


0

Isn't it largely a matter of replacing the final !d ('don't delete') with s/\n/\n@/g; ('substitute newlines by newlines plus an additional character')? $ cat file hello this is a test hi $ sed -e '1{x;$!d}' -e '/./{H;$!d;}' -e 'x;/test/ s/\n/\n@/g;' file hello @this @is @a @test hi The extra expression 1{x;$!d} just prevents an additional blank ...


0

With sed: sed -e 's/\(I|\)[^|]*$/\10758000/;s/\(A|\)[^|]*$/\10800000/' file1.txt The point is to substitute everything that comes after I| or A| (i.e. last column) with desired numbers. Result: Z89|EEE333333|100001|JANMC84|19990101|I|0758000 Z89|EEE444444|200001|JANMC84|19990101|I|0758000 Z89|EEE222222|300001|JANMC84|19990101|A|0800000 ...


1

You can do it with awk like awk -F\| 'BEGIN {OFS=FS} $6 == "A" {$7 = "0800000"} $6 == "I" {$7 = "0758000"}; 1' file1.txt This will have awk split fields based on |, then set the output field separator to also be | when we write the lines back out. Then if the sixth field, $6, is A replace the seventh field with a particular value, and a different value ...


4

Without line breaks, grep is buffering all of the input, so that it can show you the "line" where the string appears. Two questions: Do you need the adjacent content? are there spaces or other characters separating the tokens? If you don't need the context of the adjacent content and there are spaces separating tokens, just use tr to turn spaces into ...


0

$ echo '<id>I am a sample group</id>' | sed 's/<\/\?[^>]\+>//g' I am a sample group $ This will work with any tag, of course also with <a href="...">...</a> anchors. No GNUisms used — basic regex support in sed will suffice. However: please note that both opening and closing tags must be in the same line, otherwise the ...


3

When you have a command such as this sed -e ... "datafile" >"datafile" You will end up with a zero length result. The reason is that the shell sets up stdin and stdout before it executes the command. So stdout is sent to the file datafile, creating or truncating it in the process, and only then is the sed -e ... "datafile" run. A common solution is ...


1

It's only the first sed which needs modifying to handle all of the possibilities you list here. Specifically, rather than handling all possibilities in a single regexp, instead you'll need to address two different kinds of lines separately - the kind on which the ID immediately follows the first - dash on a line, and the other kind on which it is found ...


1

I implemented a small script in Ruby to solve this problem. It's used like this: source | myscript.rb | sink Here's the source $stdout.sync # no outbound buffering by Ruby buf='' $stdin.each_char do |c| if buf.length>0 || c=='<' # buffering starts when '<' received buf << c ...


0

You could use sed: sed -i.BACKUP 's/\"Ticket_.*\"/\"Ticket_Final\"/g' <xml_file> Explanation: -i.BACKUP : It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired. s/ORIGINAL_REGEXP/SUBSTITUTION/g : Substitutes (s) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g). ...


0

sed 's/T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9]\{3\}Z//' will remove a pattern like Taa:aa:aa.aaaZ, where a is any digit 0-9


0

The correct command is sed -e '/EXPRESSION_1/d' filename.txt > tempfile.txt The -e flag supplies a sed program (optional). Then the /regular-expression/ prefix says to apply the following command to all lines that match a regular expression. Then the d command (which follows) actually deletes any lines selected by the previous regular expression.


0

At least GNU sed handles input that doesn't have a newline at the end (and it produces output without a final newline if the last incomplete line is passed through). A text file under Unix must by definition end with a newline if it isn't empty, and sed is a text utility, so this leniency to non-text input is not guaranteed. Since sed is designed to ...


3

Actually, thinking about it, you can first affect all possibles: source | tr '\n<' '<\n' | paste -sd\\n - -| sed -e'/^[0-9]\{1,\}>/!{$!H;1h;$!d'\ -e\} -e'x;y/\n</<\n/;s//<&/' \ -ew\ /dev/fd/1 | filter ... | sink That will break your in-stream by first unconditionally swapping all < for \n and afterward conditionally ...


1

When a program writes to a terminal the buffer is flushed on every newline, but you can use the program unbuffer (Note in some distributions the command is stdbuf ) Try something like this unbuffer source | sed -e 's:xxxxx:yyyyy:g' | sink


2

sed -e "s/^-\([0-9][0-9][0-9][0-9]\)/\1-/" file.csv The part between \( and \) is referenced in the replacement part by \1, allowing to replace by "whatever matched the search". Note that if you are using a csv file, this will only work if the column is really the first one (^ matches the beginning of the line). If the column is somewhere else, you might ...


0

Try it with awk awk '/parent/,/\/parent/{s=s"\n"$0}/\/modelVersion/{$0=$0s}NR!=FNR' a.xml b.xml or sed with temp-file sed -n '/parent/,/\/parent/w part 1!{ /^.project/,${ /\/modelVersion/r part p } }' a.xml b.xml or with hold-space sed -n '/parent/,/\/parent/H ...


0

This should work. I take some pains to only affect the first occurence of either <parent>...</parent> and </modelVersion> *$ - else it could be made more simple. It also escapes all appended characters correctly just in case. sed -e'/^ *<parent>/!d;:n' \ -e'$d;N;/\n *<.parent>/!bn' \ -e's/[\[:space:]]/\\&/g' ...


3

The better way to change the column separator and use columncommand: sed 's/ */;/g' file | column -ts';' which produce: Item Code Active Description Store Room Row Shelf Bin On Hand 38NUTZSL Y 3/8"-16 HEX ZINC NUT B 03 C 2 0 WELD-AL Y WELD, ALUM. ROD, ...


3

an awk answer: this will keep the order the questions the same as in the source file. $ awk '{filename = "questions" ++n[$2] ".txt"; print > filename}' questions.txt $ cat questions1.txt Q.1 2 Marks Q.2 5 Marks Q.3 4 Marks Q.4 3 Marks Q.5 6 Marks $ cat questions2.txt Q.6 4 Marks Q.7 3 Marks Q.8 2 Marks Q.9 6 Marks Q.10 5 Marks


1

Figured it out. It does not like the -O grep -rlZ "wrongtext" ~/Library/Calendars | xargs sed -i '' 's/wrongtext/righttext/g'


2

That should do it: grep -rlZ "wrongtext" ~/Library/Calendars | xargs -0 sed -i '' 's/wrongtext/righttext/g' I added the -Z parameter to grep to add a zero byte instead of a newline after every filename. So the command works also with strange filenames. xargs then reads the input delimited by the zero byte with -0 and calls the sed command.



Top 50 recent answers are included