LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-27-2008, 04:55 AM   #1
nenad_m
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Rep: Reputation: 0
shell scripe replace lines in file.


Hi,

I have a file with following content:

b = 5
b = 1
b = 0
b = 13
b = 245
b = 242
stat = invalid
b = 4
b = 8
b = 0
b = 1
b = 53
stat = valid
...
b = 6
b = 8
b = 0
b = 17
b = 70
b = 113
b = 200
stat = invalid

I need file that looks like:
5, 1, 0, 13, 245, 242, invalid
4, 8, 0, 1, 53, valid
...
6, 8, 0, 17, 70, 113, 200, invalid

How to do that?
 
Old 11-27-2008, 05:14 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
What have you tried so far?
 
Old 11-27-2008, 10:18 AM   #3
nenad_m
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Well i succeed to remove characters before numbers and word 'stat', but i don't know how to group multiple rows in one row.
 
Old 11-27-2008, 10:44 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Well, using shell scripting you can simply concatenate values by putting them one after another, followed by a comma. For example, every time you read a value in a row, you can append this value to a string, then put a comma, then add the new value and so on. Here is an example of what I'm talking about:
Code:
#!/bin/bash
while read line
do
  #if echo $line | grep -q stat   # for bash version < 3
  if [[ "$line" =~ "stat" ]]      # for bash version >= 3
  then
    echo -n $number
    echo $(echo $line | cut -d ' ' -f3)
    unset number
  else
    if [ -z $number ]
    then
      number=$(echo $line | cut -d ' ' -f3),
    else
      number=$number$(echo $line | cut -d ' ' -f3),
    fi
  fi
done < testfile   # the name of your input file here
Here, you read a "line", extract the "number" and append it to the previous number (if any) followed by a comma. When you encounter a line containing the string "stat", you echo the "valid/invalid" part, unset the previous "number" and start a new line of output. Hope this helps.
 
Old 11-27-2008, 10:46 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
And here is an awk solution very similar to the previous one for the logic but using arrays:
Code:
awk '
! /stat/ {
  array[count++] = $3
}
/stat/ {
  for ( i=0; i<=count-1; i++ ){
    printf "%s,",array[i]
    delete array[i]
  }
  print $3
  count = 0
} ' testfile
 
Old 11-27-2008, 10:59 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
And another one:

Code:
awk -F' = ' 'ORS=/stat/?RS:OFS{$0=$NF}1' OFS=, infile
Or this (if you want the space after the comma):

Code:
awk -F' = ' 'ORS=/stat/?RS:OFS{$0=$NF}1' OFS=', ' infile
With Perl:

Code:
perl -ne'print/= (.*)/,/stat/?$/:", "' infile

Last edited by radoulov; 11-27-2008 at 11:07 AM.
 
  


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
Looking for a shell script that prints a file only if it has more than 10 lines djfog Linux - Newbie 8 07-01-2008 12:23 PM
Replace line in file with two lines?! Possible? eur0dad Linux - General 2 09-07-2006 02:15 PM
Deleting the lines from a file using shell scripts sharad Linux - General 1 05-22-2006 03:17 AM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 05:05 PM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 03:01 PM


All times are GMT -5. The time now is 01:37 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration