LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   insert space between 2 identical lines (https://www.linuxquestions.org/questions/linux-newbie-8/insert-space-between-2-identical-lines-4175506670/)

papori 06-01-2014 10:27 AM

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!

potato_farmer 06-01-2014 10:40 AM

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.

papori 06-01-2014 10:53 AM

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

jdkaye 06-01-2014 10:55 AM

Quote:

Originally Posted by papori (Post 5180298)
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!

Can you explain why you want to do this. For your convenience here is a possibly relevant LQ rule:
Quote:

Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
So, have far have you gone? Have you hit a stumbling block? If so, what is it?
jdk

papori 06-01-2014 11:09 AM

Quote:

Originally Posted by jdkaye (Post 5180313)
Can you explain why you want to do this. For your convenience here is a possibly relevant LQ rule:

So, have far have you gone? Have you hit a stumbling block? If so, what is it?
jdk

I am not a student.. just data analyst, but i am not use to work in linux.
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

TB0ne 06-01-2014 11:19 AM

Quote:

Originally Posted by papori (Post 5180316)
I am not a student.. just data analyst, but i am not use to work in linux. 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.

You don't say how big these files are, which makes a difference. If this is slow in R, it might be even SLOWER in bash. And as others have said, post what you have written/tried so far, and we'll be glad to help. There are many thousands of easily-found bash scripting tutorials you can find with a quick Google search...there is even one in my posting signature. But we aren't going to write your scripts for you.

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.

jdkaye 06-01-2014 11:20 AM

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

grail 06-01-2014 11:21 AM

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.

Aia 06-01-2014 09:14 PM

Quote:

Originally Posted by papori (Post 5180311)
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

Is that what you want?
Code:

awk 'p==$0{$0=ORS $0};p=$0' file

papori 06-02-2014 01:14 AM

Quote:

Originally Posted by Aia (Post 5180590)
Is that what you want?
Code:

awk 'p==$0{$0=ORS $0};p=$0' file

Thanks, but it works only in simple case.
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
while read A # read line and put in variable A.
do
    read B # read next line and put in variable B
    ((i+=1)) # increment variable i by 1
    if [ "$A" != "$B" ] # if values of variables A and B are different
    then echo -e "$A\n$B"
    else echo -e "$A\n\n$B"
    i=0 #reset the index i to 0
    fi
done < file

But, i found it to have the same problem as your script.
Any ideas?
Thanks

pan64 06-02-2014 01:51 AM

probably:
awk ' p==$0 { printf "\n" } { p=$0; print }'

grail 06-02-2014 02:28 AM

The above awk works and a simple bash script would be:
Code:

#!/bin/bash

while read -r line
do
        (( p == line )) && echo
        echo $line

        p=$line
done<in_file


pan64 06-02-2014 02:40 AM

p="$line"
would be better

papori 06-02-2014 02:45 AM

Quote:

Originally Posted by pan64 (Post 5180670)
probably:
awk ' p==$0 { printf "\n" } { p=$0; print }'

Thanks!!


All times are GMT -5. The time now is 10:46 PM.