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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to read for example the second line column by column.
Problem is that not only the field values change (except for the first column) but also the number of columns (except for the first 4).
Here's one way, using Perl. (You can obviously adjust to do other processing on the fields and to get the parametric value (the one you for) from a file or from the person running the program, etc.)
Code:
#!/usr/bin/env perl
use strict;
use warnings;
while (<>) {
if (m/^081002004779/) {
chomp; # Remove newline
# Split line on whitespace and save into @line
my @line = split /\s+/, $_;
print "In line $.:\n";
foreach my $column (@line) {
print "\tHere's a column: $column\n";
}
}
}
Also, if you know that this magic line will only occur once, you should break out of the while loop early, rather than read the rest of the file.
Last edited by Telemachos; 05-30-2009 at 06:56 PM.
Based upon your suggestion norobro i did the following as i wanted to manipulate each value as read :
Code:
#!/bin/bash
x=$(cat devicesinfo | grep 081002004779 | awk '{ print NF }')
for i in $(seq 1 $x)
do
y=$(cat devicesinfo | grep 081002004779 | awk '{j='$i'}{print $j}')
echo $y
done
your code is inefficient as it uses cat/grep/awk and at the same time calling them again in the for loop. Just one awk command will suffice. Awk replaces the use of cat/grep/sed so there is no need to use them when using awk
your code is inefficient as it uses cat/grep/awk and at the same time calling them again in the for loop. Just one awk command will suffice. Awk replaces the use of cat/grep/sed so there is no need to use them when using awk
if using bash in OP's case, no need to call wc or cut.
Code:
exec < file
read line1
read line2
set -- $line2
for items in $@
do
echo $items
done
It seems to me that neither Colucix's version nor this one does quite what the OP asked for. Colucix's treats every line the same way, ignoring the request to only process lines starting with '081002004779'. Ghostdog's assumes that the relevant line is on line 2 (and appears only once).
The OP's use of grep originally suggests that in a real sample, he may have no idea which line or lines begin with the magic '081002004779'.
well, that's because he says in his first post its the second line
Well, yes and no. He said:
Quote:
I would like to read for example the second line column by column.
Problem is that not only the field values change (except for the first column) but also the number of columns (except for the first 4).
But yeah, I didn't mean that it was hard, only that it was part of the original problem.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.