insert space between 2 identical lines
Hi all,
I am tring to insert space between 2 identical lines. input: 1 2 3 3 2 6 6 7 desire output: 1 2 3 3 2 6 6 7 Thanks! |
Assuming the values are already sorted, you can write a for loop that assigns each value to an external variable and then checks each subsequent iteration against the previously assigned value. If there is a match, it echoes the value with a new line, otherwise, it just echoes the value.
There may be an easier way to do this though. |
The numbers are just for easy example.
The input could be: aa ff ff bb and then the desire output: aa ff ff bb Thanks |
Quote:
Quote:
jdk |
Quote:
I have an outout of R script that i made(on private data), and i have to "fix" it. I know linux scripts (little bit), but not the "tricks". My problem is that the output files that i have are big, and its very slow using R. So, i dont have any smart ideas for this, but this sounds as simple problem - for one who know. Thanks |
Quote:
Since you're a data analyst, and you have written R scripts, you should already be well familiar with programming concepts. As such, you should also be easily able to use the 'tricks', such as just reading the value into a variable, then reading the next one and comparing it. If they match, read the next...if they don't, shove a line feed into the output file, reset the variable, and move on. |
I could write you one in Icon (a great string manipulation programming language) but it would be like using a canon to kill a mosquito. If you anticipate having loads of problems like the one you describe, then it might be worth having a look at Icon or Unicon. Otherwise, you'd be better off looking at a more mainstream approach.
jdk |
I see no tricks at all required here. Simple loop and an extra new line if previous equals current line. This can be done in dozens of languages and ways, so as above, please show us what
you have tried and which language you are trying to use and we will assist in a solution. |
Quote:
Code:
awk 'p==$0{$0=ORS $0};p=$0' file |
Quote:
For example, if i have: 1 1 1 1 3 42 1 2 55 55 55 7 The output is: 1 1 1 1 3 42 1 2 55 55 55 7 So, it misses when i have more than 2 sequentially. I wrote this: Code:
#!/bin/bash Any ideas? Thanks |
probably:
awk ' p==$0 { printf "\n" } { p=$0; print }' |
The above awk works and a simple bash script would be:
Code:
#!/bin/bash |
p="$line"
would be better |
Quote:
|
All times are GMT -5. The time now is 08:55 AM. |