LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-01-2014, 11:27 AM   #1
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Rep: Reputation: 0
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!
 
Old 06-01-2014, 11:40 AM   #2
potato_farmer
Member
 
Registered: May 2014
Posts: 55

Rep: Reputation: Disabled
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.
 
Old 06-01-2014, 11:53 AM   #3
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
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
 
Old 06-01-2014, 11:55 AM   #4
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
Quote:
Originally Posted by papori View Post
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
 
Old 06-01-2014, 12:09 PM   #5
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jdkaye View Post
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
 
Old 06-01-2014, 12:19 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,921

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by papori View Post
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.
 
Old 06-01-2014, 12:20 PM   #7
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 06-01-2014, 12:21 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
1 members found this post helpful.
Old 06-01-2014, 10:14 PM   #9
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
Originally Posted by papori View Post
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
 
Old 06-02-2014, 02:14 AM   #10
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Aia View Post
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
 
Old 06-02-2014, 02:51 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
probably:
awk ' p==$0 { printf "\n" } { p=$0; print }'
 
1 members found this post helpful.
Old 06-02-2014, 03:28 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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
 
Old 06-02-2014, 03:40 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
p="$line"
would be better
 
Old 06-02-2014, 03:45 AM   #14
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
probably:
awk ' p==$0 { printf "\n" } { p=$0; print }'
Thanks!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to insert lines into fstab Cerephim Linux - Server 4 10-14-2013 03:31 PM
[SOLVED] remove similar (not identical) lines of text steve51184 Linux - Software 5 04-25-2012 12:45 AM
logwatch has identical entries for Disk Space txref Linux - Software 3 04-16-2010 12:20 PM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 05:37 AM
remove identical lines in a file benjithegreat98 Linux - General 4 04-24-2004 07:12 AM


All times are GMT -5. The time now is 10:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration