Filtering Multiple variables from a single column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filtering Multiple variables from a single column
# 1  
Old 11-15-2010
Filtering Multiple variables from a single column

Hi,

I am currently filtering a file, "BUILD_TIMES", that has multiple column of information in it. An example of the data is as follows;

Code:
Fri Nov 5 15:31:33 2010 00:28:17 R7_BCGNOFJ_70.68
Fri Nov 5 20:57:41 2010 00:07:21 R7_ADJCEL_80.6
Wed Nov 10 17:33:21 2010 00:01:13 R7_BCTTEST3_80.1X

I am using the "awk" command to filter the columns so i get the following output;

Code:
Fri Nov 5 2010 00:28:17 BCGNOFJ
Fri Nov 5 2010 00:07:21 ADJCEL
Wed Nov 10  2010 00:01:13 BCTTEST3

The awk command i am using is as follows;

Code:
cat $BUILD_TIMES | awk '{split($NF,a,"_");print $1,$2,$3,$5,$6,a[2]}' >> $FINAL_LIST

Is there any way to filter out the "X" in the "R7_BCTTEST3_80.1X" column so that i would get the output in this format;

Wed Nov 10 2010 00:01:13 BCTTEST3 X

or in this format

Wed Nov 10 2010 00:01:13 BCTTEST3X

Any help would be greatly appreciated.

Thanks in advance.
Moderator's Comments:
Mod Comment
Please use code tags when posting data/code samples

Last edited by Franklin52; 11-15-2010 at 08:38 AM.. Reason: Code tags, please!
# 2  
Old 11-15-2010
sed method Smilie
Code:
# sed 's/[0-9]*:[0-9]*:[0-9]* //;s/\(.*\) .*_\(.*\)_.*\(.\)$/\1 \2 \3/' infile 
Fri Nov 5 2010 00:28:17 BCGNOFJ 8
Fri Nov 5 2010 00:07:21 ADJCEL 6
Wed Nov 10 2010 00:01:13 BCTTEST3 X

# 3  
Old 11-15-2010
Code:
sed 's/[^ \t]*[ \t]//4;s/[^ \t_]*_//;s/_.*\(.\)$/ \1/;s/[^X]$//' infile

Code:
Fri Nov 5 2010 00:28:17 BCGNOFJ
Fri Nov 5 2010 00:07:21 ADJCEL
Wed Nov 10 2010 00:01:13 BCTTEST3 X

---------- Post updated at 14:34 ---------- Previous update was at 14:27 ----------

Code:
awk -F'[ \t_]*' '{print $1,$2,$3,$5,$6,$8($9~/X$/?" X":x)}' infile

# 4  
Old 11-15-2010
Hi Scrutinizer,

I used your sed command and it works very well except i still have one problem. The three lines of code

Code:
Fri Nov 5 15:31:33 2010 00:28:17 R7_BCGNOFJ_70.68
Fri Nov 5 20:57:41 2010 00:07:21 R7_ADJCEL_80.6
Wed Nov 10 17:33:21 2010 00:01:13 R7_BCTTEST3_80.1X

i showed earlier are part of a 1001 line file. The format of each line is exactly the same. However, when i run the sed command the first 180 or so lines come out like the following;

Code:
Sat Oct  06:37:31 2010    00:30:21 CMS
Sat Oct  06:38:48 2010    00:30:24 WRANMOMFJ
Sat Oct  06:40:30 2010    00:33:14 WRANMOMFJ
...

i.e. the time of day and not the date is being printed. The rest of the lines come out perfectly. Any suggestions on how to fix this? Could i use a "for" loop or a "do" loop to go through each individual line?

Any help would be greatly appreciated.

Thanks in advance Smilie
# 5  
Old 11-15-2010
Try:
Code:
sed 's/[^ \t][^ \t]*[ \t]//4;s/[^ \t_]*_//;s/_.*\(.\)$/ \1/;s/[^X]$//' infile

Does the awk work?
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-15-2010
That is perfect Scrutinizer. Is there a site where i could find out how each part of that command works?

The awk command did not work i kept getting a syntax error.

Thank you so much again though for the help.
# 7  
Old 11-15-2010
Though I did not try your command, I know there are some issues with
calling awk on some platforms. Therefore, if you are porting your code
to different platforms you may want to call this function than replace awk with $AWK

Code:
 
which_awk()
{
   if [ -n "$SCRIPT_PATH" ]
   then
         eval $SCRIPT_PATH
   fi
   whence nawk > /dev/null
   if [ $? -eq 0 ]
   then
      AWK=nawk
   else
      whence awk > /dev/null
      if [ $? -eq 0 ]
      then
         AWK=awk
      else
         AWK=gawk
      fi
   fi
}


Last edited by radoulov; 11-15-2010 at 10:42 AM.. Reason: Fixed code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning multiple column's value from Oracle query to multiple variables in UNIX

Hi All, I need to read values of 10 columns from oracle query and assign the same to 10 unix variables. The query will return only one record(row). I tried to append all these columns using a delimiter(;) in the select query and assign the same to a single variable(V) in unix. I thought I... (3 Replies)
Discussion started by: hkrishnan91
3 Replies

2. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

3. Shell Programming and Scripting

Multiple columns to a single column

I have this input: 10 22 1 100 11 22 10 1 50 14 3 1 100 23 3 1 100 24 15 1 100 10 22 5 3 1 33.333 11 22 1 100 It has an inconsistent number of fields but the last field is determined by 100/(NF-2) using awk. I want to take this multiple columned input file and transform so that... (2 Replies)
Discussion started by: mdlloyd7
2 Replies

4. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

5. Shell Programming and Scripting

Filtering issues with multiple columns in a single file

Hi, I am new to unix and would greatly appreciate some help. I have a file containing multiple colums containing different sets of data e.g. File 1: John Ireland 27_December_69 Mary England 13_March_55 Mike France 02_June_80 I am currently using the awk... (10 Replies)
Discussion started by: crunchie
10 Replies

6. UNIX for Dummies Questions & Answers

Multiple column into single column

Hi , i have an output as follows... xxx yyyy aaaa bbb and i want it to be printed as xxx yyyy aaaa bbb Any help please... (3 Replies)
Discussion started by: appu2176
3 Replies

7. Shell Programming and Scripting

Filtering multiple files with variables

Hi, I spend few hours already searching this forum, but cannot find the solution matching exactly my case. I have multiple log files, I need to filter them so I get info about certain event. So we have files: LOGA.txt LOGB.txt LOGC.txt LOGD.txt LOGE.txt 1. I need to grep lines in... (10 Replies)
Discussion started by: Vitoriung
10 Replies

8. UNIX for Dummies Questions & Answers

single column to multiple columns

Hello, I have a single column of data that I would like to cut/print (with awk or ...) into multiple columns at every empty row (or common character). Input: 5.99123 5.94693 7.21383 5.95202 0.907935 5.99149 6.08427 0.975774 6.077 Output: 5.99123 5.95202 6.08427 5.94693... (7 Replies)
Discussion started by: agibbs
7 Replies

9. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies

10. Shell Programming and Scripting

Help needed in processing multiple variables in a single sed command.

Is it possible to process multiple variables in a single sed command? I have the following ksh with three variables and I want to search for all variables which start with "var" inside input.txt. I tired "$var$" but it just prints out everyting in input.txt and does not work. $ more test.ksh... (5 Replies)
Discussion started by: stevefox
5 Replies
Login or Register to Ask a Question