Reading files of different type and same basename | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reading files of different type and same basename

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-24-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 21 October 2014, 10:12 AM EDT
Location: Goa / Bangalore, India
Posts: 1,597
Thanks: 77
Thanked 519 Times in 474 Posts
Reading files of different type and same basename

Hi ! all

I have need of accessing multiple files of different type same base name, and I want to compare base name if matching then I want to send those 2 files of different type to some program, for further processing

my files are like this

Code:
file_1.txt
file_2.txt
file_3.txt
file_4.txt
file_6.txt
file_8.txt


Code:
file_1.asc
file_2.asc
file_3.asc
file_4.asc
file_7.asc

First I want to save file names in some array like this


Code:
file_type1 array
file_type2 array


Code:
now compare file_type1 array and file_type2 array

first send base name matched files as a variable to sub program say
Code:
variable name txt for .txt file

and
Code:
variable name asc for .asc file

program is something like this


Code:
awk .................'{}' $txt 
awk ..................'{}' $asc

once matched file ends, then send only one variable from unmatched file array, to sub program one by one

expected output

if I do echo $txt $asc should result like below


Code:
file_1.txt file_1.asc
file_2.txt file_2.asc
file_3.txt file_3.asc
file_4.txt file_4.asc
file_6.txt
file_7.asc
file_8.txt

Hope this will be possible with your help

Last edited by Akshay Hegde; 03-24-2013 at 05:49 AM..
Sponsored Links
    #2  
Old 03-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 9:27 AM EDT
Location: Aachen, Germany
Posts: 4,406
Thanks: 73
Thanked 1,076 Times in 1,015 Posts
Not sure I understood your sophisticated specification, but as a starting point you could evaluate this one:
Code:
ls *.txt *.asc |
  cut -d\. -f1 |
  uniq -c -w7  |
  while read CNT FILE
    do [ "$CNT" -eq 2 ] && { ufct1 "${FILE}.asc"; ufct1 "${FILE}.txt"; } || ufct2 "$FILE*"
    done
file_1.asc
file_1.txt
file_2.txt
file_3.asc
file_3.txt
file_4.asc
file_4.txt
file_6.txt
file_7.asc
file_8.txt

having defined user shell functions ufctn to just echo the parameter.
The Following User Says Thank You to RudiC For This Useful Post:
Akshay Hegde (03-24-2013)
Sponsored Links
    #3  
Old 03-24-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 21 October 2014, 10:12 AM EDT
Location: Goa / Bangalore, India
Posts: 1,597
Thanks: 77
Thanked 519 Times in 474 Posts
Sir
Code:
ufctn

function not working with my OS(Ubuntu 12.04 LTS) but I could is info page

following is error msg

Code:
ufct1: command not found
ufct1: command not found
ufct2: command not found
ufct1: command not found
ufct1: command not found
ufct2: command not found
ufct2: command not found
ufct2: command not found

any alternate solution is possible ?
    #4  
Old 03-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 9:27 AM EDT
Location: Aachen, Germany
Posts: 4,406
Thanks: 73
Thanked 1,076 Times in 1,015 Posts
Quote:
Originally Posted by RudiC View Post
. . . having defined user shell functions ufctn to just echo the parameter.
Did you ever hear about shell functions? You may call them subroutines as well... man bash:
Quote:
Shell Function Definitions
A shell function is an object that is called like a simple command and executes a compound command with a new set of positional
parameters. Shell functions are declared as follows:

name () compound-command [redirection]
function name [()] compound-command [redirection]
This defines a function named name. The reserved word function is optional.
I'm not sure what else you need? Want it spoon fed?
Code:
$ ufct1() { echo $1 double; }
$ ufct2() { echo $1 single; }

Put your processing into those functions.
Sponsored Links
    #5  
Old 03-24-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 21 October 2014, 10:12 AM EDT
Location: Goa / Bangalore, India
Posts: 1,597
Thanks: 77
Thanked 519 Times in 474 Posts
Sir I need $1 as separate variable here what happens is first txt file will come then asc file

suppose if I use
Code:
$1

in my awk script that is


Code:
# 1st script
awk .....'{}' $1

#2nd script
awk ...'{gsub(" ","\t");print}' $1

then both script will work with same input

I want like this


Code:
# 1st iteration 

some computation using file_1.txt
# 1st script
awk .....'{}' file_1.txt

#2nd script
some computation using file_1.asc
awk ...'{gsub(" ","\t");print}' file_1.asc

I did following mistake(like subroutine) sorry Sir
and thank you so much for solution



Code:
if statements
abc;


abc()
{
xyz code 
}


Last edited by Akshay Hegde; 03-24-2013 at 08:08 AM..
Sponsored Links
    #6  
Old 03-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 9:27 AM EDT
Location: Aachen, Germany
Posts: 4,406
Thanks: 73
Thanked 1,076 Times in 1,015 Posts
Not sure I understand. Why don't you simply call functionA with the .txt file, and then functionB with the .asc file as a parameter?
Sponsored Links
    #7  
Old 03-24-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 21 October 2014, 10:12 AM EDT
Location: Goa / Bangalore, India
Posts: 1,597
Thanks: 77
Thanked 519 Times in 474 Posts
Sir there are only 2 ways left to me as there are many comparison inside script

one is use txt and asc as separate variable

2nd one is merge base name matching file and write id number to file

assume file_1.txt looks like this


Code:
15-12-1996   21-2-2024 12 34,000 888 976  1
15-12-1994   21-2-2026 12 35,000 885 977  2
15-12-1993   21-2-2025 12 37,000 887 976  3
15-12-1995   21-2-2029 12 39,000 878 976  4

and file_1.asc looks like this

Code:
15-12-1996   21-2-2024 12 34,000 888 976  1
15-12-1995   21-2-2029 12 39,000 878 976  4
15-12-1994   21-2-2026 12 35,000 885 977  2
15-12-1993   21-2-2025 12 37,000 887 976  3
15-12-1994   21-2-2026 12 35,000 885 977  2
15-12-1993   21-2-2025 12 37,000 887 976  3

then merged file should look like this

Code:
15-12-1996   21-2-2024 12 34,000 888 976  1 id1
15-12-1994   21-2-2026 12 35,000 885 977  2 id1
15-12-1993   21-2-2025 12 37,000 887 976  3 id1
15-12-1995   21-2-2029 12 39,000 878 976  4 id1
15-12-1996   21-2-2024 12 34,000 888 976  1 id2
15-12-1995   21-2-2029 12 39,000 878 976  4 id2
15-12-1994   21-2-2026 12 35,000 885 977  2 id2
15-12-1993   21-2-2025 12 37,000 887 976  3 id2
15-12-1994   21-2-2026 12 35,000 885 977  2 id2
15-12-1993   21-2-2025 12 37,000 887 976  3 id2

out of 2 any one solution can complete my task
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
basename + file with space jagnikam Shell Programming and Scripting 5 02-10-2010 10:21 AM
Basename a file with spaces linge Shell Programming and Scripting 2 12-02-2008 05:22 AM
array type has incomplete element type jaganadh Programming 1 07-24-2007 03:54 AM
Human readable type vs MIME type detection using file spauldingsmails Shell Programming and Scripting 0 03-22-2007 12:43 AM



All times are GMT -4. The time now is 11:10 AM.