Sponsored Content
Top Forums Shell Programming and Scripting Enhance existing script: Extract Multiple variables & Input in an echo string Post 303042199 by nk1984 on Wednesday 18th of December 2019 09:41:28 PM
Old 12-18-2019
Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts

I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative.

Input: file
Code:
{"house":"1024","zip":"2345","city":"asd","country":"zzv"}
{"city":"asd","house":"1024","zip":"2845","country":"zzv"}
{"house":"1028","zip":"2645","city":"asd","country":"zzv"}
{"zip":"2545","house":"1021","city":"asd","country":"zzv"}
{"city":"asd","house":"1020","zip":"2345","country":"zzv"}

Script:
Code:
for i in `cat file`
do
	HNO=`echo $i | awk -F"\"house\":" '{print $2}'| cut -d"," -f1 | sed 's/\"//g'`
	ZIP=`echo $i | awk -F"\"zip\":" '{print $2}'| cut -d"," -f1 | sed 's/\"//g'`
	CIT=`echo $i | awk -F"\"city\":" '{print $2}'| cut -d"," -f1 | sed 's/\"//g'`
	echo 'House number is '$HNO', Zip Code is '$ZIP', City is '$CIT' and Country is zzv'
done

Output:
Code:
House number is 1024, Zip Code is 2345, City is asd and Country is zzv
House number is 1024, Zip Code is 2845, City is asd and Country is zzv
House number is 1028, Zip Code is 2645, City is asd and Country is zzv
House number is 1021, Zip Code is 2545, City is asd and Country is zzv
House number is 1020, Zip Code is 2345, City is asd and Country is zzv

Thanks for your help in advance
Regards
Nk
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

extract from string variable into new variables

I have a variable which consists of a string like this: 001 aaabc 44 a bbb12 How do I extract each substring, delimited by the spaces, into new variables - one for each substring? eg var1 will be 001, var2 will be aaabc, var3 will be 44, var4 will be a, etc? I've come up with this:... (2 Replies)
Discussion started by: Sniper Pixie
2 Replies

2. Shell Programming and Scripting

Extract numbers from a string and store in variables

Hi All, Is it possible in Unix shell script to extract numbers from a string containing ".", such as; 5.2.314 And store in variables so; var1 = 5 var2 = 2 var3 = 314 Thanks in advance for any help anyone can provide dave (6 Replies)
Discussion started by: davewg
6 Replies

3. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

4. Shell Programming and Scripting

Tailing new log file & echo the string on console

Guys, I do have a script that runs to take the server out from network, after running the script it is writing the new log file{outFile} in to directory . Now what i need is my script should tail the last modified file{outFile} & search the string {Server Status} ans should echo the same at the... (0 Replies)
Discussion started by: raghunsi
0 Replies

5. Shell Programming and Scripting

Multiple Variables in Array from Existing Directories

I would like to extract directories from a specific place and keep them into an array of variables to run functions into it. Example, bash-3.00$ls adrian bryan caren derrick I want to keep each directory names into a variable adrian --> document bryan --> document caren --> document... (3 Replies)
Discussion started by: lynxlee
3 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

8. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

9. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

10. Shell Programming and Scripting

Extract multiple values into corresponding variables

Here is my input # MANIFEST.MF Manifest-Version: 1.0 Build-Jdk: 1.6.0 Built-By: CM_TEAM Build_SvnRev: 662789 Build_Number: 13.0.0.0-JDK8 Build_Date: Wed 04/05/2017-20:48:19.17 Archiver-Version: Plexus Archiver Created-By: Apache Maven 3.1.0 Here is the expected output:... (4 Replies)
Discussion started by: kchinnam
4 Replies
Archive::Zip::MemberRead(3)				User Contributed Perl Documentation			       Archive::Zip::MemberRead(3)

NAME
Archive::Zip::MemberRead - A wrapper that lets you read Zip archive members as if they were files. SYNOPSIS
use Archive::Zip; use Archive::Zip::MemberRead; $zip = Archive::Zip->new("file.zip"); $fh = Archive::Zip::MemberRead->new($zip, "subdir/abc.txt"); while (defined($line = $fh->getline())) { print $fh->input_line_number . "#: $line "; } $read = $fh->read($buffer, 32*1024); print "Read $read bytes as :$buffer: "; DESCRIPTION
The Archive::Zip::MemberRead module lets you read Zip archive member data just like you read data from files. METHODS
Archive::Zip::Member::readFileHandle() You can get a "Archive::Zip::MemberRead" from an archive member by calling "readFileHandle()": my $member = $zip->memberNamed('abc/def.c'); my $fh = $member->readFileHandle(); while (defined($line = $fh->getline())) { # ... } $fh->close(); Archive::Zip::MemberRead->new($zip, $fileName) Archive::Zip::MemberRead->new($zip, $member) Archive::Zip::MemberRead->new($member) Construct a new Archive::Zip::MemberRead on the specified member. my $fh = Archive::Zip::MemberRead->new($zip, 'fred.c') setLineEnd(expr) Set the line end character to use. This is set to by default except on Windows systems where it is set to . You will only need to set this on systems which are not Windows or Unix based and require a line end diffrent from . This is a class method so call as "Archive::Zip::MemberRead"->"setLineEnd($nl)" rewind() Rewinds an "Archive::Zip::MemberRead" so that you can read from it again starting at the beginning. input_record_separator(expr) If the argumnet is given, input_record_separator for this instance is set to it. The current setting (which may be the global $/) is always returned. input_line_number() Returns the current line number, but only if you're using "getline()". Using "read()" will not update the line number. close() Closes the given file handle. buffer_size([ $size ]) Gets or sets the buffer size used for reads. Default is the chunk size used by Archive::Zip. getline() Returns the next line from the currently open member. Makes sense only for text files. A read error is considered fatal enough to die. Returns undef on eof. All subsequent calls would return undef, unless a rewind() is called. Note: The line returned has the input_record_separator (default: newline) removed. read($buffer, $num_bytes_to_read) Simulates a normal "read()" system call. Returns the no. of bytes read. "undef" on error, 0 on eof, e.g.: $fh = Archive::Zip::MemberRead->new($zip, "sreeji/secrets.bin"); while (1) { $read = $fh->read($buffer, 1024); die "FATAL ERROR reading my secrets ! " if (!defined($read)); last if (!$read); # Do processing. .... } AUTHOR
Sreeji K. Das, <sreeji_k@yahoo.com> See Archive::Zip by Ned Konz without which this module does not make any sense! Minor mods by Ned Konz. COPYRIGHT
Copyright 2002 Sreeji K. Das. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2009-06-30 Archive::Zip::MemberRead(3)
All times are GMT -4. The time now is 01:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy