Sponsored Content
Top Forums Shell Programming and Scripting ksh: how to extract strings from each line based on a condition Post 302607422 by angie1234 on Wednesday 14th of March 2012 11:10:57 AM
Old 03-14-2012
ksh: how to extract strings from each line based on a condition

Hi ,
I'm a newbie.Never worked on Unix before. I want a shell script to perform the following:
I want to extract strings from each line ,based on the type of line(Nameline,Subline) and output it to another file.Below is a sample format.

Code:
2010-12-21 14:00"1"Nameline"Midterm"First Name:Jane     Last Name: Doe      "
2010-12-21 14:00"2"Subline"Midterm"Subject:Mathematics                     "
2010-12-21 14:00"3"Markline"Midterm"Int.Marks:48   Ext.Marks:92             "
2010-12-21 14:00"4"Subline"Midterm"Subject:Physics                         "
2010-12-21 14:00"5"Markline"Midterm"Int.Marks:30   Ext.Marks:86             "
2010-12-21 14:00"6"Subline"Midterm"Subject:Chemistry                       "
2010-12-21 14:00"7"Markline"Midterm"Int.Marks:45   Ext.Marks:98             "
2011-04-22 09:00"1"Nameline"Final"First Name:John     Last Name: Smith    "
2010-04-22 09:00"2"Subline"Final"Subject:Mathematics                     "
2010-04-22 09:00"3"Markline"Final"Int.Marks:30   Ext.Marks:76             "
2010-04-22 09:00"4"Subline"Final"Subject:Physics                         "
2010-04-22 09:00"5"Markline"Final"Int.Marks:32   Ext.Marks:70             "
2010-04-22 09:00"6"Subline"Final"Subject:Chemistry                       "
2010-04-22 09:00"7"Markline"Final"Int.Marks:41   Ext.Marks:88             "

From the Nameline, string such a Jane and Doe should be extracted and be outputted to a new file in the format shown as below.The fields that are not applicable are to be left blank for the line. The structure and length of the last column is same for a particular type of line, for ex., every first name starts at position 12 of the last column and is of length 8 and every last name starts at position 31 and is of length 9. Similiarly for other lines the position of the values to be extracted are fixed.

Code:
<<Exam_Date,Exam_Time,Line_Type,Exam_Type,Student_FName,Student_LName,Subject,Int_Marks,Ext_Marks>>
2010-12-21,14:00,Nameline,Midterm,Jane,Doe,   ,   ,   "
2010-12-21,14:00,Subline,Midterm,   ,   ,Mathematics,   ,   "
2010-12-21,14:00,Markline,Midterm,   ,   ,   ,48,92"
2010-12-21,14:00,Subline,Midterm,   ,   ,Physics,   ,   "
2010-12-21,14:00,Markline,Midterm,   ,   ,   ,30,86"
2010-12-21,14:00,Subline,Midterm,   ,   ,Chemistry,   ,   "
2010-12-21,14:00,Markline,Midterm,   ,   ,   ,45,98"

Can anyone please help me out.
Thanks!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to extract multiple strings from a line

Hi I have the following requirement. i have the following line from a log file one : two : Three : four : five : six : seven : eight :nine :ten Now can you pls help what i should do to get only the following output from the above line two : five : six : seven : Eight appreciate your... (3 Replies)
Discussion started by: vin_eme
3 Replies

2. Shell Programming and Scripting

extract xml tag based on condition

Hi All, I have a large xml file of invoices. The file looks like below: <INVOICES> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>1234</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>2345</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME>... (9 Replies)
Discussion started by: angshuman
9 Replies

3. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

4. Shell Programming and Scripting

ksh : Building an array based on condition result

I want to build an Errorlog. I would like to build an array as I move through the if statements and print the array once all error conditions have been defined. The results need to be comma delimited. tsver will be static "1.9.6(2)" other vars $prit $lt $rt can have the same or a different... (1 Reply)
Discussion started by: popeye
1 Replies

5. Shell Programming and Scripting

Multi line extraction based on condition

Hi I have some data in a file as below ****************************** Class 1A Students absent are : 1. ABC 2. CDE 3. CPE ****************************** Class 2A Students absent are : ****************************** Class 3A Students absent are : (6 Replies)
Discussion started by: reldb
6 Replies

6. UNIX for Dummies Questions & Answers

Extract strings based on the value

I have a file with multiple columns (in this case, the file has 3 columns): NM_001006304 (-33.7) XM_418228 (-38.4) JN880447 (-33.7) CR387600 (-33.7) CR524203 (-36.3) GALGA_6AKII_KRT75 (-33.7) GALGA25_SC7 (-31.9) CR352795 (-36.3) NM_204172 (-31.7) NM_204137 (-31.9) NM_001030561 (-36.3) AB011672... (7 Replies)
Discussion started by: yuejian
7 Replies

7. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

8. Shell Programming and Scripting

Extract batch based on condition

HI, I have a file as mentioned below. Here one batch is for one user id.Batch starts from |T row and ends at .T row. I want to create a new file by reading this file. The condition is for record 10(position 1-2), if position 3 to position 17 is 0 then delete the entire batch and write into the new... (9 Replies)
Discussion started by: abhi.mit32
9 Replies

9. Shell Programming and Scripting

Extract multiple strings from line

Hello I have an output that has a string between quotes and another between square brackets on the same line. I need to extract these 2 strings Example line Device "nrst3a" attributes=(0x4) RAW SERIAL_NUMBER=SNL2 Output should look like nrst3a VD073AV1443BVW00083 I was trying with sed... (3 Replies)
Discussion started by: bombcan
3 Replies

10. Shell Programming and Scripting

Remove line based on condition in awk

In the following tab-delimited input, I am checking $7 for the keyword intronic. If that keyword is found then $2 is split by the . in each line and if the string after the digits or the +/- is >10, then that line is deleted. This will always be the case for intronic. If $7 is exonic then nothing... (10 Replies)
Discussion started by: cmccabe
10 Replies
exit(1)                                                            User Commands                                                           exit(1)

NAME
exit, return, goto - shell built-in functions to enable the execution of the shell to advance beyond its sequence of steps SYNOPSIS
sh exit [n] return [n] csh exit [ ( expr )] goto label ksh *exit [n] *return [n] DESCRIPTION
sh exit will cause the calling shell or shell script to exit with the exit status specified by n. If n is omitted the exit status is that of the last command executed (an EOF will also cause the shell to exit.) return causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command exe- cuted. csh exit will cause the calling shell or shell script to exit, either with the value of the status variable or with the value specified by the expression expr. The goto built-in uses a specified label as a search string amongst commands. The shell rewinds its input as much as possible and searches for a line of the form label: possibly preceded by space or tab characters. Execution continues after the indicated line. It is an error to jump to a label that occurs between a while or for built-in command and its corresponding end. ksh exit will cause the calling shell or shell script to exit with the exit status specified by n. The value will be the least significant 8 bits of the specified status. If n is omitted then the exit status is that of the last command executed. When exit occurs when executing a trap, the last command refers to the command that executed before the trap was invoked. An end-of-file will also cause the shell to exit except for a shell which has the ignoreeof option (See set below) turned on. return causes a shell function or '.' script to return to the invoking script with the return status specified by n. The value will be the least significant 8 bits of the specified status. If n is omitted then the return status is that of the last command executed. If return is invoked while not in a function or a '.' script, then it is the same as an exit. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
break(1), csh(1), ksh(1), sh(1), attributes(5) SunOS 5.10 15 Apr 1994 exit(1)
All times are GMT -4. The time now is 01:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy