Sponsored Content
Top Forums Shell Programming and Scripting Bash: Pulling first and last character in string Post 302962323 by durden_tyler on Friday 11th of December 2015 07:13:47 PM
Old 12-11-2015
If Perl is an option in your system, then here's a solution:

Code:
$ 
$ # list the files in current directory
$ ls -1
compare_and_print.pl
comp_file
well_list
$ 
$ # show the contents of the "well_list" and "comp_file" files
$ cat well_list
TEST_WELL_01
TEST_WELL_02
TEST_WELL_11
TEST_WELL_22
GOV_WELL_1
GOV_WELL_201
PUB_WELL_57
PUB_WELL_82
$ 
$ cat comp_file
/drive/t/Asset/13_Wells/Test_Well_1/LAS_and_LIS/LL3.LIS
/drill/t/Cnt-143/Wells/Gov_Well_201/Drilling/Government_Well_Mnemonics.dlis
/drill/t/Cnt-143/Wells/Gov_Well_201/Drilling/LL3_0893780002.LIS
/drive/t/Asset/13_Wells/Test_Well_011/LAS_and_LIS/LL3_9367000.LIS
/drive/r/Asset/13_Wells/Test_Well_01/LAS_and_LIS/LL3_9367000.LIS
/drill/t/Cnt-143/Wells/Gov_Well_01/Drilling/Government_Well_Mnemonics.dlis
/drive/w/Asset/13_Wells/Public_Well_82/County_82/NEUT&DN.LIS
$ 
$ # show the Perl program to compare these files and append results
$ cat -n compare_and_print.pl
     1	#!/usr/bin/perl -w
     2	use strict;
     3	
     4	# A hash to store the file names read from "well_list"
     5	my %files;
     6	my $file1 = "well_list";
     7	my $file2 = "comp_file";
     8	
     9	# Read "well_list" and set up a hash key that can be compared easily with
    10	# "comp_file". For a line like "TEST_WELL_01", the hash key would be
    11	# "test_well_1" i.e. lower case for the text joined with the integer value of
    12	# the last number. The value of this key is the actual line itself, since we
    13	# need this to create the file of this name. Thus, after reading line
    14	# "TEST_WELL_01" the hash would look like this:
    15	#     $files{"test_well_1"} = "TEST_WELL_01"
    16	open(FH, "<", $file1) or die "Can't open $file1: $!";
    17	while (<FH>) {
    18	    chomp;
    19	    my $val = $_;
    20	    /^(.*)_(.*?)$/;
    21	    my $key = lc($1)."_".int($2);
    22	    $files{$key} = $val;
    23	}
    24	close(FH) or die "Can't open $file1: $!";
    25	
    26	# Now read "comp_file". Split each line on "/" character. Then transform the
    27	# 5th token, the well name, as per the transformation rule above. That is,
    28	# lower case the text part joined with the integer value of the last number.
    29	# If this key exists in the hash %files, then open the file name derived
    30	# from the hash value and append the line we just read into the file.
    31	open(FH, "<", $file2) or die "Can't open $file2: $!";
    32	while (<FH>) {
    33	    chomp;
    34	    my $line = $_;
    35	    my @tokens = split("\/");
    36	    my ($txt, $num) = $tokens[5] =~ /^(.*)_(.*?)$/;
    37	    my $cmp = lc($txt)."_".int($num);
    38	    if (defined $files{$cmp}) {
    39	        print "Well found, appending to file: ", $line, "\n";
    40	        open(FH1, ">>", $files{$cmp}) or die "Can't open $files{$cmp}: $!";
    41	        print FH1 $line."\n";
    42	        close(FH1) or die "Can't close $files{$cmp}: $!";
    43	    } else {
    44	        print "Well not found               : ", $line, "\n";
    45	    }
    46	}
    47	close(FH) or die "Can't close $file2: $!";
    48	
$ 
$ # Run the Perl program
$ perl compare_and_print.pl
Well found, appending to file: /drive/t/Asset/13_Wells/Test_Well_1/LAS_and_LIS/LL3.LIS
Well found, appending to file: /drill/t/Cnt-143/Wells/Gov_Well_201/Drilling/Government_Well_Mnemonics.dlis
Well found, appending to file: /drill/t/Cnt-143/Wells/Gov_Well_201/Drilling/LL3_0893780002.LIS
Well found, appending to file: /drive/t/Asset/13_Wells/Test_Well_011/LAS_and_LIS/LL3_9367000.LIS
Well found, appending to file: /drive/r/Asset/13_Wells/Test_Well_01/LAS_and_LIS/LL3_9367000.LIS
Well found, appending to file: /drill/t/Cnt-143/Wells/Gov_Well_01/Drilling/Government_Well_Mnemonics.dlis
Well not found               : /drive/w/Asset/13_Wells/Public_Well_82/County_82/NEUT&DN.LIS
$ 
$ 
$ # now check if new files were created by the Perl program
$ ls -1
compare_and_print.pl
comp_file
GOV_WELL_1
GOV_WELL_201
TEST_WELL_01
TEST_WELL_11
well_list
$ 
$ # check the contents of the new files
$ cat GOV_WELL_1
/drill/t/Cnt-143/Wells/Gov_Well_01/Drilling/Government_Well_Mnemonics.dlis
$ 
$ cat GOV_WELL_201
/drill/t/Cnt-143/Wells/Gov_Well_201/Drilling/Government_Well_Mnemonics.dlis
/drill/t/Cnt-143/Wells/Gov_Well_201/Drilling/LL3_0893780002.LIS
$ 
$ cat TEST_WELL_01
/drive/t/Asset/13_Wells/Test_Well_1/LAS_and_LIS/LL3.LIS
/drive/r/Asset/13_Wells/Test_Well_01/LAS_and_LIS/LL3_9367000.LIS
$ 
$ cat TEST_WELL_11
/drive/t/Asset/13_Wells/Test_Well_011/LAS_and_LIS/LL3_9367000.LIS
$ 
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash script to check the first character in string

Hello would appreciate if somebody can post a bash script that checks if the first character of the given string is equal to, say, "a" thnx in advance (2 Replies)
Discussion started by: ole111
2 Replies

2. Shell Programming and Scripting

Pulling the first and last character/number from a string.

Let's say I have a word "foobar23" in a file, and I want to pull the first "f" and last "3" character out of the world, how would I accomplish that? # cat file foobar23 I want the output to be: f3 (3 Replies)
Discussion started by: LinuxRacr
3 Replies

3. Shell Programming and Scripting

Bash script pulling variable from query_string

I have a variable embedded in a long string called "commands" coming in on a query_string. I need to copy the string to a file with the variable $loop1 converted before it writes. Right now, it writes the varible itself instead of what it should be. QUERY_STRING... (2 Replies)
Discussion started by: numele
2 Replies

4. Shell Programming and Scripting

Bash - get specific character from the string

Hi! If I want to extract a character from a specific position of a string, I can use ${string:1:1} (if I want character at the position 1). How can I do the same thing, when the number of position is contained in the variable? ${string:$var:1}doesn't work, unfortunately. Thanks in advance. (2 Replies)
Discussion started by: xqwzts
2 Replies

5. Shell Programming and Scripting

Bash: How to remove the last character of a string?

In bash, how can one remove the last character of a string? In perl, the chop function would remove the last character. However, I do not know how to do the same job in bash. Many thanks in advance. (12 Replies)
Discussion started by: LessNux
12 Replies

6. Shell Programming and Scripting

Basic bash -- pulling files from an FTP server

Hi guys. Very new to this so apologies if this is ridiculously obvious, but I am not sure why this isn't working. I want to pull a file off an FTP server. I currently do it through windows, which is no problem, but I want to move everything to a Linux box I just installed. wget won't work as the... (4 Replies)
Discussion started by: majormajormajor
4 Replies

7. Shell Programming and Scripting

BASH- Need help pulling data from .emlx

Hello, fellow computer junkies. First time poster! My boss wrote an application (Mavericks 10.9, Mountain Lion 10.8) that checks a user's security settings. The user runs the application, then it spits out an email that is sent back to our inbox showing the results. On our end, we have a mail rule... (5 Replies)
Discussion started by: sudo
5 Replies

8. Shell Programming and Scripting

Match string against character class in bash

Hello, I want to check whether string has only numeric characters. The following code doesn't work for me #!/usr/local/bin/bash if ]]; then echo "true" else echo "False" fi # ./yyy '346' False # ./yyy 'aaa' False I'm searching for solution using character classes, not regex.... (5 Replies)
Discussion started by: urello
5 Replies

9. Shell Programming and Scripting

Bash - Inserting non printable character(s) in string variable

Hello. I have a string variable named L_TEMP to test a very simple filter. L_TEMP="50AwL.|KWp9jk" I want to insert a non printable character between K and W. I have try this : linux-g65k:~ # a='50AwL.|K' linux-g65k:~ # b='Wp9jk' linux-g65k:~ # L_TEMP="$a$'\x07'$b" linux-g65k:~ # echo... (6 Replies)
Discussion started by: jcdole
6 Replies

10. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies
All times are GMT -4. The time now is 12:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy