ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.