Help Needed Using awk/CUT


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Needed Using awk/CUT
# 1  
Old 01-18-2014
Help Needed Using awk/CUT

Hi Experts,

I am writing a script and struct at a part Need your help to get this

I have a file generated called /tmp/testify.log

Code:
$ cat testify.log

Machine Parts                   6       DREE
Mufler Strengths        33              XYNC
Siscos                  20      09      ABSC


$ cat /tmp/testify.log | grep -v '^$'|awk 'BEGIN { FS = "[ \t]+" } {for(i=1;i<=NF;i++) print $i;} END {print " "}'
Machine                                        ###>>#here the Column1 for Row 1 is lining into 2 seperate but i want them into a single  also the last column has null values jut want to replace null with -
Parts
6
DREE

Mufler
Strengths
33
XYNC

Siscos
20
09
ABSC


how can i acheive the required output... the anticipated output . .

Code:
Machine Parts
-
6
DREE

Mufler Strengths
33
-
XYNC

Siscos
20
09
ABSC

Help Needed Using awk/CUT-untitledpng

Last edited by Don Cragun; 01-19-2014 at 12:21 AM.. Reason: Add CODE tags.
# 2  
Old 01-19-2014
The question is what the format is of the input file. If it is TAB separated then something like this may do:
Code:
awk -F'\t' '{for(i=1; i<=NF; i++) print ($i=="")?"-":$i; print ""}' file

(don't use [\t]+, since it will "eat" empty fields)

But your attached file looks like it may be fixed position format (with spaces) and then you would need something else.
# 3  
Old 01-19-2014
The following seems to do what you want if the input file contains tabs, spaces, or a combination of spaces and tabs between fields. It assumes that the fields reside in fixed character positions and that tab stops are set after every eight characters:
Code:
awk '
function extract_field(first, last,     string) {
        string = substr($0, first, last - first)
        sub(/ *$/, "", string)
        return string == "" ? "-" : string
}
/^$/ {  next
}
/\t/ {  # Convert tabs in input to spaces assuming tab stops are set in columns
        # 9 + 8x.
        while(i = index($0, "\t")) $0 = substr($0, 1, i - 1) \
                sprintf("%.*s", 8 - (i - 1) % 8, "        ") substr($0, i + 1)
}
{       # At this point, field 1 is characters 1-24, field 2 is characters
        # 25-32, field 3 is characters 33-40, and field 4 is columns 41-EOL.
        printf("%s%s\n%s\n%s\n%s\n", oc++ ? "\n" : "", extract_field(1,24),
                extract_field(25,32), extract_field(33,40),
                extract_field(41,80))
}' testify.log

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 4  
Old 01-19-2014
Could this help you ?

Code:
$ cat file
Machine Parts                   6       DREE
Mufler Strengths        33              XYNC
Siscos                  20      09      ABSC

Code:
awk  '

function spr(){
                  n=split($0,A,r) 
                  for(i=1;i<=n;i++)
                      {
                        so = so ? so A[i] : A[i]
                        if((i in W) || n == i)
                           {
                             print so ~ /[[:alnum:]]|[[:punct:]]/ ? so : "-"  
                             so = ""
                           }
                      }
                  printf FNR < C ? RS : NULL
              }

       FNR==NR{
                   j=0
                   n=split($0,A,r)
                   for(i=1; i<=n; i++)
                     {
                     if(f==0 && A[i]!=" ")
                               {
                                  s=i
                                  f=1
                               }
                     if( f == 1 && A[i] != " " && A[i+1]== " " && A[i+2] == " " || i == n)
                               {
                                  f=0
                                  ++j
                                  WI[FNR,j] = s     
                               }
                     }
        
                     # Consider width of max NF
                         if(j>p){
                                   mf  = j
                                   max = FNR
                                }
                       p = j
                       C = FNR
                       next
              }

        FNR==1{
                     for(i=1; i<=mf; i++)
                     W[(WI[max,i]-1) != 0 ? WI[max,i] -1 : -1 ]
              }

              {
                       spr()
              }

    '  file file

Resulting
Code:
Machine Parts           
-
6       
DREE

Mufler Strengths        
33      
-
XYNC

Siscos                  
20      
9      
ABSC

# 5  
Old 01-22-2014
Don - Thanks for your awk but i used my inputfile but still things are not working as expected

this is my input file... since the tab after each feild is differnt its touch to handle

any better was is helpfull

Code:
AB SISCO Transport SEED           2,189,675       4,308      2   52.3 USO
AB XPU                                            2,854          34.6
Dreed pinch Feed                    308,043         811      3    9.8 UIO
PX MNC: AB Load Del Info                950         189    199    2.3 Other
MN SISCO Transport FEED             215,085         165           2.0 ION
AB-TRANS SISCO Transport REED       105,321         127      1    8.1 SIO NB


Last edited by itsme488; 01-22-2014 at 12:23 AM..
# 6  
Old 01-22-2014
I guess that I'm not surprised that code that was written to extract 4 left-justified fields from an input line doesn't correctly extract 6 fields (some left-justified and some right-justified) with completely different field alignments. If you give us sample input that is not representative of your real input, you're wasting all of our time. With your new sample input, the following updated awk script does something that I am guessing is closer to what you want, but (since you didn't show what the output should be), it is just a guess:
Code:
awk '
function extract_field(first, last,     string) {
        string = substr($0, first, last - first + 1)
        sub(/^ */, "", string)
        sub(/ *$/, "", string)
        return string == "" ? "-" : string
}
/^$/ {  next
}
/\t/ {  # Convert tabs in input to spaces assuming tab stops are set in columns
        # 9 + 8x.
        while(i = index($0, "\t")) $0 = substr($0, 1, i - 1) \
                sprintf("%.*s", 8 - (i - 1) % 8, "        ") substr($0, i + 1)
}
{       # At this point, field 1 is characters 1-32, field 2 is characters
        # 33-43, field 3 is characters 44-55, and field 4 is characters 56-62,
        # field 5 is characters 63-69, and field 6 is characters 70-EOL.  All
        # of these fields offsets are guesses based on provided sample input.
        printf("%s%s\n%s\n%s\n%s\n%s\n%s\n",
                oc++ ? "\n" : "",
                extract_field(1,32),
                extract_field(33,43),
                extract_field(44,55),
                extract_field(56,62),
                extract_field(63,69),
                extract_field(70,100))
}' testify.log2

When your new sample input is saved in a file named testify.log2, the above script produces the output:
Code:
db file scattered read
2,189,675
4,308
2
52.3
User I/O

DB CPU
-
2,854
-
34.6
-

direct path read
308,043
811
3
9.8
User I/O

PX Nsq: PQ load info query
950
189
199
2.3
Other

db file sequential read
215,085
165
1
2.0
User I/O

control file sequential read
105,321
127
1
8.1
System I/O

Of course, there are no tabs in the sample input you've shown us. And, if there is a tab at the end of each field but the last, your tab stops are not set at multiples of eight character boundaries.

Is that what you want?
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-22-2014
Don : I don't think the new sample input provided in #5 is real one.. as I noticed input changed 2-3 times in post, I too felt it's just waste of time.

I think we should start questioning "what you have tried so far ?" before answering.. I seen many members posting input and expected output logging out, (very few are posting attempt towards thread) coming back after sometime, copy paste and again logout, if not answering or answer delayed sending private messages, dumping up / cross posting, etc. I think for this type of dumping up cases infractions are not just enough, we should make something like login denied for some hours, its my opinion.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

2. Shell Programming and Scripting

help with cut command needed

I have this file containing 8 veritcal lines and I need to cut the first two lines into a new file and then cut the next two lines into a new file and so on... any help would be much appreciated. I tried the cut -c but that doesnt work and I am not sure what else to try. Thanks. (1 Reply)
Discussion started by: drew211
1 Replies

3. Shell Programming and Scripting

Want to use awk instead of cut

I want to use awk instead of cut command. Following is my code: line="slNumber=US78AJF11643, slName=USJFKAAUSYDAAUL80441032900095, dummy sl found? sqlca.sqlcode=0" sl_WORD=`echo $line| cut -f 1 -d','` sl=`echo $sl_WORD | cut -f 2 -d'='` echo "$sl" Please suggest me about the code. ... (5 Replies)
Discussion started by: rinku
5 Replies

4. Shell Programming and Scripting

HELP! using cut/awk

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. how would i do this using cut or awk methods is it possbile and having a output similar to this x, number of apperances = y, number of apperances = (2 Replies)
Discussion started by: deadleg
2 Replies

5. UNIX for Dummies Questions & Answers

Help please awk or cut

Hi I'm new to unix programming so struggling with something thats probably simple to many of you I have data files of the format : ID, date, value1, value2, blank on each line either value1 or value2 will be zero. I need my output file to contain ID, date, non-zero value The input... (3 Replies)
Discussion started by: thewench
3 Replies

6. Shell Programming and Scripting

Is awk vs cut which one is better

i was trying to work on program to look for users never log on sever.. using awk with awk is working last| awk '{print $1}' |sort -u > /tmp/users1$$ cat /etc/passwd | awk -F: '{print $1}' |sort -u > /tmp/users2$$ comm -13 /tmp/users$$ rm -f /tmp/users$$ with cut it is not working ... (3 Replies)
Discussion started by: macrules
3 Replies

7. Shell Programming and Scripting

awk or cut

select some fields from data file (source.csv) The data in file(source.csv) is like "x1,2",,"y",,"z" How to get the 1st, 2nd and 3rd field from the file. Using awk or cut? Note: "x1,2" is one field. thanks, (16 Replies)
Discussion started by: anypager
16 Replies

8. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies

9. UNIX for Dummies Questions & Answers

Cut help needed!!!!!!

cut help needed!!!!! -------------------------------------------------------------------------------- /home/documents/files/scooter17.dat I am trying to cut out everything upto scooter17.dat so that it will only print out the scooter17.dat any ideas? great advices will be greatly... (1 Reply)
Discussion started by: scooter17
1 Replies

10. UNIX for Dummies Questions & Answers

cut help needed!!!!!

/home/documents/files/scooter17.dat I am trying to cut out everything upto scooter17.dat so that it will only print out the scooter17.dat any ideas? great advices will be greatly appreciated. Thank you ********Updated*************** Thank you for the replys. It works!!... (4 Replies)
Discussion started by: scooter17
4 Replies
Login or Register to Ask a Question