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
Locale::Language(3perl) 				 Perl Programmers Reference Guide				   Locale::Language(3perl)

NAME
Locale::Language - standard codes for language identification SYNOPSIS
use Locale::Language; $lang = code2language('en'); # $lang gets 'English' $code = language2code('French'); # $code gets 'fr' @codes = all_language_codes(); @names = all_language_names(); DESCRIPTION
The "Locale::Language" module provides access to standard codes used for identifying languages, such as those as defined in ISO 639. Most of the routines take an optional additional argument which specifies the code set to use. If not specified, the default ISO 639 two- letter codes will be used. SUPPORTED CODE SETS
There are several different code sets you can use for identifying languages. The ones currently supported are: alpha-2 This is the set of two-letter (lowercase) codes from ISO 639, such as 'he' for Hebrew. This code set is identified with the symbol "LOCALE_LANG_ALPHA_2". This is the default code set. alpha-3 This is the set of three-letter (lowercase) bibliographic codes from ISO 639, such as 'heb' for Hebrew. This code set is identified with the symbol "LOCALE_LANG_ALPHA_3". term This is the set of three-letter (lowercase) terminologic codes from ISO 639. This code set is identified with the symbol "LOCALE_LANG_TERM". ROUTINES
code2language ( CODE [,CODESET] ) language2code ( NAME [,CODESET] ) language_code2code ( CODE ,CODESET ,CODESET2 ) all_language_codes ( [CODESET] ) all_language_names ( [CODESET] ) Locale::Language::rename_language ( CODE ,NEW_NAME [,CODESET] ) Locale::Language::add_language ( CODE ,NAME [,CODESET] ) Locale::Language::delete_language ( CODE [,CODESET] ) Locale::Language::add_language_alias ( NAME ,NEW_NAME ) Locale::Language::delete_language_alias ( NAME ) Locale::Language::rename_language_code ( CODE ,NEW_CODE [,CODESET] ) Locale::Language::add_language_code_alias ( CODE ,NEW_CODE [,CODESET] ) Locale::Language::delete_language_code_alias ( CODE [,CODESET] ) These routines are all documented in the Locale::Codes man page. SEE ALSO
Locale::Codes Locale::Constants http://www.loc.gov/standards/iso639-2/ Source of the ISO 639 codes. AUTHOR
See Locale::Codes for full author history. Currently maintained by Sullivan Beck (sbeck@cpan.org). COPYRIGHT
Copyright (c) 1997-2001 Canon Research Centre Europe (CRE). Copyright (c) 2001-2010 Neil Bowers Copyright (c) 2010-2011 Sullivan Beck This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2011-09-26 Locale::Language(3perl)
All times are GMT -4. The time now is 01:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy