Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

removing line breaks, and operating on an entire stream

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
ansient
Guru
Guru
User avatar
Posts: 445
Joined: Sat Jan 22, 2005 10:41 pm
Location: Argentina

removing line breaks, and operating on an entire stream

  • Quote

Post by ansient » Sun Jun 26, 2005 2:07 pm

Suppose I have a file with the following:

Code: Select all

key1
value1
key2
value2
key3
value3
...
And I want to change this to:

Code: Select all

key1	value1
key2	value2
key3	value3
...
Is there an easy way to do this with bash/sed/awk?

Secondly, is there a way to do something like:

Code: Select all

sed 's/....\n..../.....foo..../g'
And have it operate on the entire stream instead of line-by-line?
devmanual

I've lost a machine.. literally _lost_. it responds to ping, it works completely, I just can't figure out where in my apartment it is. -- http://bash.org/?5273
Top
karnesky
Apprentice
Apprentice
Posts: 218
Joined: Thu Mar 18, 2004 9:07 pm
Contact:
Contact karnesky
Website

  • Quote

Post by karnesky » Sun Jun 26, 2005 2:56 pm

If the key and value are differentiated in some way, just improve your matching expression. If they aren't, but they are always on every other line, make a shell script which loops over every line & has different behavior for odd and even lines.
Donate to F/OSS
Top
ansient
Guru
Guru
User avatar
Posts: 445
Joined: Sat Jan 22, 2005 10:41 pm
Location: Argentina

  • Quote

Post by ansient » Sun Jun 26, 2005 5:11 pm

karnesky wrote:If the key and value are differentiated in some way, just improve your matching expression.
I can easily match either a particular key or a value, but how can I match both at once in order to output them on the same line?
If they aren't, but they are always on every other line, make a shell script which loops over every line & has different behavior for odd and even lines.
So...

Code: Select all

#!/bin/bash
declare lastline
declare count=1;
while read line; do
    if [ $(($count % 2)) = 0 ]; then
        echo -e "$lastline\t$line"
    fi
    let $((count++));
    lastline="${line}";
done < $@
Is that really the best way? :?
devmanual

I've lost a machine.. literally _lost_. it responds to ping, it works completely, I just can't figure out where in my apartment it is. -- http://bash.org/?5273
Top
TrueDFX
Retired Dev
Retired Dev
Posts: 1348
Joined: Wed Jun 02, 2004 5:33 pm

  • Quote

Post by TrueDFX » Sun Jun 26, 2005 5:44 pm

Code: Select all

while read line1
do
    read line2
    printf '%s\t%s\n' "$line1" "$line2"
done
Is that close enough to what you want?

Or

Code: Select all

sed 'N;s/\n/\t/'
ansient wrote:Secondly, is there a way to do something like:

Code: Select all

sed 's/....\n..../.....foo..../g'
And have it operate on the entire stream instead of line-by-line?

Code: Select all

sed -e '
    :start
# after the last line is read in, go to the part where you can modify your text
    $bmodify
# otherwise, read in the next line
    N
# and go back (until the whole file is read)
    bstart
# here, the whole file is in memory
    :modify
# so you can search/replace things like this
    s/A\nB/replaced/g
'
But, there almost always is a nicer way which won't suddenly fail if files get too big.
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic