Solved

Command to see particular colum value in the flat file

Posted on 2004-03-29
15
388 Views
Last Modified: 2010-05-18
Hi ,

I need to find 50th column value in the flatfile.There a header for flatfile and there is separator || to separe for each field.could you please let me know what is command which can resolve this .
0
Comment
Question by:sumanth_ora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
15 Comments
 
LVL 7

Expert Comment

by:fim32
ID: 10706640
you want to find the value on that line?

well, you can:
awk -F| '{print $50}'

where | is the separator
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 10707954
fim32

The separator is || not |, and the field separator for awk/cut can only be a single character (AFAIK).  Also note that you need to quote the pipe symbol.

A workaround is:

sed "s/||/|/g" flatfile | awk -F'|' '{print $50}'
0
 
LVL 62

Expert Comment

by:gheist
ID: 10708160
Kind of work for cut

cut -b 1-10 <onefile > anotherfile

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:fim32
ID: 10708180
ah, i wasn't sure if that were a typo, or a separate odd character...

technically tho, even if || were the separator (and good point on the quoting), you could still just use awk (every other field would be empty), but i think the limit on awk fields is 99?
0
 
LVL 9

Expert Comment

by:Alf666
ID: 10708273
This might not work. There's a chance that your file contains pipe signs (|) inside the records (hence the double pipe).

This means that sometimes, you will not get the proper field.

The following will work for sure.

perl -n -e 'chomp ; @arr=split(/\|\|/); print $arr[50] . "\n";'
0
 
LVL 9

Expert Comment

by:Alf666
ID: 10708282
Err. Forgot the file name :

perl -n -e 'chomp ; @arr=split(/\|\|/); print $arr[50] . "\n";' flatfile
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10709336
If using Perl, we can make that a little shorter :-)

perl -F'\|\|' -lane 'print $F[49]' flatfile
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10713068
dooh, we see a lot of experts testing on linux instead Unix (see TA :-))

>  but i think the limit on awk fields is 99
no, the limit is 9
only gawk, probably nawk or mawk,  can have more

KISS as Tintin ;-)
0
 
LVL 9

Expert Comment

by:Alf666
ID: 10713647
BTW, thanks Tintin, I had a small typo :

perl -n -e 'chomp ; @arr=split(/\|\|/); print $arr[49] . "\n";' flatfile

But your solution is simpler.
0
 
LVL 20

Expert Comment

by:Gns
ID: 10721961
Ehm, guys... Testing on an AIX 5.2 awk... The file aa is as the above, containg lines like "a||a||a||a||...b||c||" with the b in column 50...:
(first the "non working case of a single | delimiter)
>awk -F'|' '{print $100}' aa


>awk -F'|' '{print $101}' aa  
c
c
>awk -F'|' '{print $99}' aa
b
b
(And now, the interresting case, where the "character" is the "||" _string_)
>awk -F'||' '{print $49}' aa
a
a
>awk -F'||' '{print $50}' aa
b
b
>
The first case (singel "|" delimiter) was exactly repeatable on my (retired) DG/UX machines, but not the latter ("||" delimiter). On solaris 2.6 awk complains on "to many fields in line ..." while the 2.6 nawk bugs out over the delimiter (illegal primary in RE || at |)...
... Conclusions: Depending on your awk implementation you might not have the arbitrary limits preventing the simple awk from working.
The nice perl scriptlets abive will (of course) do the job.

-- Glenn
0
 
LVL 6

Expert Comment

by:bira
ID: 10723735
"I need to find 50th column value in the flatfile"

Suppose flatfile has:
abcdefghijklnmopkrstuvxabcdedfghijklnmopqrstuvxzxO||abcdefghijk

  Run as below:

 cat flatfile|cut -f1 -d'||'|rev|cut -c1-1
0
 
LVL 6

Expert Comment

by:bira
ID: 10723865
Or this way, that dont´t depend on separators:

 cat flatfile|cut -c50-50|rev|cut -c1-1
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10726374
bira, which cut allows a string as separator for -c ?
also nice interpretation of the question ;-) which happens when people are to lazy to explain ..
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10727775
sumanth_ora.

Could you please provide some feedback and possibly close this question.
0
 
LVL 20

Expert Comment

by:Gns
ID: 10730758
:-) Achim and bira... And CC to Achims question... When I tried, I of course tried all OSes at hand with a cut solution too... None seem to work with a string delimiter. I might add that AIX awk manpage does indeed explain FS values as either being blank, singel char or an extended regular expression (which explains why it works;).

-- Glenn
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question