Sponsored Content
Top Forums Shell Programming and Scripting Bash: Pulling first and last character in string Post 302962318 by petfyp on Friday 11th of December 2015 06:00:38 PM
Old 12-11-2015
Bash: Pulling first and last character in string

I am writing a bash script that will find all references to the “Well_List” in the “Comp_File”.

I am filtering a Well_List that contains the following:
Code:
 TEST_WELL_01
 TEST_WELL_02
 TEST_WELL_11
 TEST_WELL_22
 GOV_WELL_1
 GOV_WELL_201
 PUB_WELL_57
 PUB_WELL_82
 . 
 .

Comparison File contains (Comp_File):
Code:
 /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
 .
 .

I’ve gone through many of the existing submissions on your webpage, but can’t find what I need. Unfortunately, I’m fairly new to sed which I figured will be needed here. I can perform this task individually (see Code below), but need to get both to perform the search I need in “Comp_File”
Code:
Last field of Well_List line: 

for x in `cat Well_List; do; awk –F_ ‘{print $NF}’; done

Code:
First field: 
 for x in `cat Well_List; do; awk '{print substr($0,1,1)}'; done

Although I couldn’t resolve the difference between “Test_Well_1” and Test_Well_01” in my script, which both belong under the same Well Name.

Each comparison result will be placed in an individual file that will be titled after the “Well_List” Well Name (example: TEST_WELL_01.txt)

I hope I didn’t confuse anyone with this. I’ve spent most of today trying to figure this out…..

Could anyone provide some assistance in getting this done? If additional information is needed, please let me know.

Thanks!

Last edited by Scrutinizer; 12-12-2015 at 04:15 AM.. Reason: Removed spurious formatting
 

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 06:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy