Find Data in test file and write each out to a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Data in test file and write each out to a line
# 8  
Old 10-26-2015
So - what's your question?

---------- Post updated at 15:03 ---------- Previous update was at 14:58 ----------

This what I get from my proposal and your latest data:
Code:
D;SecondName1;FirstName1;;F;;;Clubname;9;;70;;M;1;22;;;;
D;SecondName1;FirstName1;;F;;;Clubname;9;;100;;M;1;22;;;;
D;SecondName1;FirstName1;;F;;;Clubname;9;;200;;M;1;22;;;;
D;SecondName1;FirstName1;;F;;;Clubname;9;;400;;M;1;22;;;;
D;SecondName2;FirstName2;;F;;;Clubname;8;;70;;M;1;23;;;;
D;SecondName2;FirstName2;;F;;;Clubname;8;;100;;M;1;23;;;;
D;SecondName2;FirstName2;;F;;;Clubname;8;;200;;M;1;23;;;;
D;SecondName2;FirstName2;;F;;;Clubname;8;;400;;M;1;23;;;;
D;SecondName3;FirstName3;;F;;;Clubname;11;;70;;M;1;24;;;;
D;SecondName3;FirstName3;;F;;;Clubname;11;;100;;M;1;24;;;;
D;SecondName3;FirstName3;;F;;;Clubname;11;;700s;;M;1;24;;;;
D;SecondName3;FirstName3;;F;;;Clubname;11;;DT;;M;1;24;;;;
D;SecondName3;FirstName3;;F;;;Clubname;11;;SP.JT;;M;1;24;;;;

Note the "KL" field is NOT found in your data.
# 9  
Old 10-26-2015
You can't get an exit code of -1 from a process run by the shell.

Your comments about the format of center.txt described in your comments and the format of center.txt implied by the comments in your code do not match.

The output format you said you wanted and the echo statements in your code do not match. I don't think the echo statements in the code below are correct either, but I hope they are close.

And having two nested loops reading from the same file can't work.

Perhaps the following will come closer to what you want???
Code:
#!/bin/bash
# set initial variables
# ask for the input filename from Zenity
file="$(zenity --file-selection --title="Select a Wardells Input File")"
case $? in
(0)	echo "\"$file\" selected.";;
(1)	echo "No file selected."
	exit 1;;
(*)	echo "An unexpected error has occurred."
	exit 2;;
sac

IFS=","
name=${file%.*}	# finds the Name part of the input filename
fileevt="$name.evt"	# Output filename for event listing
outfile="$name.txt"	# Output filenme

echo "the input file name to use will be $file"
echo $name
echo $outfile

mmcodes=1		# have MM codes been done?  Not used in this script...

rm -f "$outfile"	# Delete Output file

#**********************************************************************
#Main Program code
#**********************************************************************
#extract event numbers from line
while read A cname B athnum firstname lastname gender age REST
do	echo "Name = $firstname"
	echo "Surname = $lastname"
	# read the Center Name and ShortName from  External file.  format is:
	#	CenterName,CenterShortName,CenterNumber
	read X csname centnum < <(grep "^${cname}," centers.txt)

	for event in $REST
	do	[ ! "$event" ] && continue
		echo "D;$lastname;$firstname;;$gender;$cname;;$csname;$centnum;$age;;$event;;M;1;$athnum;;;;"      
		echo "D;$lastname;$firstname;;$gender;$cname;;$csname;$centnum;$age;;$event;;M;1;$athnum;;;;" >> "$outfile"
	done
done < "$file"		# specify the import file to work on

This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 10-26-2015
[QUOTE=Don Cragun;302958814]You can't get an exit code of -1 from a process run by the shell.
Ok thanks


And having two nested loops reading from the same file can't work.
Ok thanks That was one of my origfinal concerns

Perhaps the following will come closer to what you want???
Very close Thank you

2 Questions.
When I run the code as you posted, the last line of each section is split into 2 if the input line has a space after the last event, it adds an extra line with no $event code in it

how does the script select only the event?

Last edited by kcpoole; 10-26-2015 at 07:53 PM.. Reason: Wrong files quoted
# 11  
Old 10-26-2015
Quote:
Originally Posted by kcpoole
Quote:
Originally Posted by Don Cragun
You can't get an exit code of -1 from a process run by the shell.
Ok thanks


And having two nested loops reading from the same file can't work.
Ok thanks That was one of my origfinal concerns

Perhaps the following will come closer to what you want???
Very close Thank you

2 Questions.
When I run the code as you posted, the last line of each section is split into 2

if the input line has a space after the last event, it adds an extra line with no $event code in it
Without seeing the split output lines and the input line that led to that output, I don't see any obvious reason why the last line would be split.

If you change the line in the in the script:
Code:
	do	[ ! "$event" ] && continue

to:
Code:
	do	[ ! "${event%[[:space:]]}" ] && continue

it will ignore any event that is just a <space> or <tab> character.

Last edited by Don Cragun; 10-26-2015 at 08:24 PM.. Reason: Add missing "}"; s/"${event%[[:space:]]"/"${event%[[:space:]]}"/
# 12  
Old 10-26-2015
Quote:
Originally Posted by Don Cragun
Without seeing the split output lines and the input line that led to that output, I don't see any obvious reason why the last line would be split.

If you change the line in the in the script:
Code:
    do    [ ! "$event" ] && continue

to:
Code:
    do    [ ! "${event%[[:space:]]" ] && continue

it will ignore any event that is just a <space> or <tab> character.
is that correct format? adding that gives an error on Line 39 @ 40 ( line 39 is the file input line)
./test: line 39: unexpected EOF while looking for matching `"'

Last edited by kcpoole; 10-26-2015 at 08:08 PM..
# 13  
Old 10-26-2015
Quote:
Originally Posted by kcpoole
is that correct format? adding that gives an error on Line 39 @ 40 ( line 39 is the file input line)
./test: line 39: unexpected EOF while looking for matching `"'
Sorry, I missed a closing brace:
Code:
	do	[ ! "${event%[[:space:]]}" ] && continue

I'll correct it in my earlier post.
This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 10-26-2015
Awesome thanks That fixed that particular problem

The only issue now is the lines that have no trailing comma. in that case the line is split after the $event code.

Code:
D;SecondName1;FirstName1;;F;Clubname;;CLB;900;9;;200;;M;1;22;;;;
D;SecondName1;FirstName1;;F;Clubname;;CLB;900;9;;400
;;M;1;22;;;;

I added an extra blank column in the original spreadsheet, but then would have to remember to do that every time i refreshed the data

Ken
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

2. Shell Programming and Scripting

Extract data from XML file and write in CSV file

Hi friend i have input as following XML file <?xml version="1.0"?> <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.02"> <BkToCstmrDbtCdtNtfctn> <GrpHdr><MsgId>LBP-RDJ-TE000000-130042430010001001</MsgId><CreDtTm>2013-01-04T03:21:30</CreDtTm></GrpHdr>... (3 Replies)
Discussion started by: mohan sharma
3 Replies

3. Shell Programming and Scripting

Write over data to new file

hi..i would ask about how to write over data to new file with BASH. so..assume my data looks like this : 11 12 13 14 15 ...and so on. It's always line by line. and that's for the first file. i want to write over those numbers into second file but by using space. so my second file should be... (5 Replies)
Discussion started by: syalala
5 Replies

4. Shell Programming and Scripting

Find matches and write the data before it

Hi all I am here for help once again I have two files One file is like this with one columns F2 B2 CAD KGM HTC CSP Second file is like this in 5 columns where firs column contain sometime entries of first file with space and other entries (12 Replies)
Discussion started by: Priyanka Chopra
12 Replies

5. Shell Programming and Scripting

Find common entries in 2 list and write data before it

Hi all, I have 2 files: second file I want if entries in one file will match in other file. It shuld wite approve before it so output shuld be (1 Reply)
Discussion started by: manigrover
1 Replies

6. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

7. Programming

How to write data to file in C?

Hi I want to open a file and write data in the following manner. Header String 1 String 2 String 3 String 4 String 5 ... (4 Replies)
Discussion started by: AAKhan
4 Replies

8. Shell Programming and Scripting

Find line number of bad data in large file

Hi Forum. I was trying to search the following scenario on the forum but was not able to. Let's say that I have a very large file that has some bad data in it (for ex: 0.0015 in the 12th column) and I would like to find the line number and remove that particular line. What's the easiest... (3 Replies)
Discussion started by: pchang
3 Replies

9. Hardware

how to write data into a device file?

Hi, I am working in device drivers. I am new to device drivers. i have invoked chardev.c. the driver is insmoded. now i want to write something into this and i want to look what i have written. but i don't know how to write and see. please help me (0 Replies)
Discussion started by: boidi
0 Replies

10. Shell Programming and Scripting

Extract data from an XML file & write into a CSV file

Hi All, I am having an XML tag like: <detail sim_ser_no_1="898407109001000090" imsi_1="452070001000090"> <security>ADM1=????</security> <security>PIN1=????</security> <security>PIN2=????</security> ... (2 Replies)
Discussion started by: ss_ss
2 Replies
Login or Register to Ask a Question