Extracting strings at various positions of text file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extracting strings at various positions of text file
# 1  
Old 03-27-2017
Extracting strings at various positions of text file

Hi Team -

I hope everyone has been well!

I export a file from one of our source systems that gives me more information than I need. The way the file outputs, I need to extract certain strings at different positions on the file and echo them to another file.

I can do this in batch easily, but having trouble in shell. The file is attached.

If you open in Notepad++, you'll see the 4 positions(columns on row 9).

Application Database Variable Value (in that order)

I need to extract the values under 'Variable' & 'Variable' and echo them to another text file.

Can someone help me? Having trouble grabbing those values. In batch, I first find the string in Application column I'm looking for, sometimes there is more than 1 application, then I move over 2 & 3 positions to grab the other values.

Thanks!
# 2  
Old 03-27-2017
How about
Code:
awk '
BEGIN           {SRCH = SUBSEP
                }

/^MAXL/         {sub (/;$/, _)
                 SRCH = $NF
                }

$1 ~ SRCH       {gsub (/  */, " ")
                 print $2 " = " $3
                }

' /tmp/mozilla_coerdtr0/Display_Variables.txt 
ScenarioCurrent = Fcst
FcstYear1 = FY17
FcstYear2 = FY18
FcstYear3 = FY19
FcstYear4 = FY20
FcstYear5 = FY21
FcstMonth = Jan
ScenarioPrior = Fcst_Q1
ActualMonth = Jan
ActualYear = FY17

# 3  
Old 03-27-2017
Hi RudiC -

Thank you so much! Unforunately, I'm able to produce your results.

here is my code:

Code:
#!/bin/bash

awk '
BEGIN           {SRCH = SUBSEP
                }

/^MAXL/         {sub (/;$/, _)
                 SRCH = $NF
                }

$1 ~ SRCH       {gsub (/  */, " ")
                 print $2 " = " $3
                }

' /home/oracle/Hyperion_Batch/Scripts/Batch/Display_Variables.txt

It's not modifying the file at all.

Essentially, as you have it, I on'y want to following to be spooled to a new file:

Quote:
ScenarioCurrent = Fcst
FcstYear1 = FY17
FcstYear2 = FY18
FcstYear3 = FY19
FcstYear4 = FY20
FcstYear5 = FY21
FcstMonth = Jan
ScenarioPrior = Fcst_Q1
ActualMonth = Jan
ActualYear = FY17
No other content...

Any ideas? Thanks!

---------- Post updated at 10:31 AM ---------- Previous update was at 09:43 AM ----------

This gives me the first portion, but I also need the next position over from this output:

Code:
awk '{for (I=1;I<=NF;I++) if ($I == "MS_IND") {print $(I+1)};}' test.txt > new_file.txt

Quote:
ScenarioCurrent
FcstYear1
FcstYear2
FcstYear3
FcstYear4
FcstYear5
FcstMonth
ScenarioPrior
ActualMonth
ActualYear
Anyway to amend this?

Thanks!


Update:

I got it:

Code:
awk '{for (I=1;I<=NF;I++) if ($I == "MS_IND") {print $(I+1)","$(I+2)};}' test.txt > new_file.txt

Thanks!

Last edited by SIMMS7400; 03-27-2017 at 10:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

2. Shell Programming and Scripting

Extracting text between two strings, multiple instances

Hi experts, Ive got a text file which has the following text which will occur in this format at least one time: +=========================>> Some stuff that evreryone should knnow other stufsjdokajkajokajda aijhjajcdjajcisajcqsqdqwdqad <<=========================+ It is likely that... (8 Replies)
Discussion started by: martin0852
8 Replies

3. Shell Programming and Scripting

Extracting text between two constant strings

Hi All, I have a file whose common patter is like this: .I 1 .U 87049087 .S Some text here too .M This is a text .T Some another text here .P Name of the book .W Some lines of more text. This text needs to be extracted. .A more text goes here too .I 2 (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

4. Shell Programming and Scripting

output strings to specific positions in a file

Been searching for about 3 hours for similar functionality that I can get examples of how to output text from variables into certain locations in a file. I would like to incorporate this into a script. I have not been able to find a command example that does it all in one method. I find part of... (1 Reply)
Discussion started by: bennu_500
1 Replies

5. UNIX for Dummies Questions & Answers

find positions of a letter in a text file

Hi, I would like to know how can I get all the positions of a letter, let say letter C in a text file. sample input file: hcck pgog hlhhc desired output file: 2 3 13 Many thanks! (2 Replies)
Discussion started by: fadista
2 Replies

6. Shell Programming and Scripting

Extracting text between two strings

Hi, I've looked at a few existing posts on this, but they don't seem to work for my inputs. I have a text file where I want to extract all the text between two strings, every time that occurs. Eg my input file is Anna said that she would fetch the bucket. Anna and Ben moved the bucket.... (9 Replies)
Discussion started by: JamesForeman
9 Replies

7. Shell Programming and Scripting

Extracting text between two strings, first instance only

There are a lot of ways to extract text from between two strings, but what if those strings occur multiple times and you only want the text from the first two strings? I can't seem to find anything to work here. I'm using sed to process the text after it's extracted, so I prefer a sed answer, but... (4 Replies)
Discussion started by: fubaya
4 Replies

8. Shell Programming and Scripting

Replace 9-16 positions of a text file.

Hi i am having text file like this 40000201040005200213072009000000700000050744820906904421 40069300240005200713072009000000067400098543630000920442 i want to replace 9-16 positions of my txt file...by 1234567...in a single line command i.e 0400052....should be replaced by... (2 Replies)
Discussion started by: suryanarayana
2 Replies

9. Shell Programming and Scripting

extracting a set of strings from a text file

i have textfiles that contain a series of lines that look like this: string0 .................................................... column3a column4a string1**384y0439 ..................................... column3b column4b... (2 Replies)
Discussion started by: Deanne
2 Replies

10. Shell Programming and Scripting

Help with extracting strings from a file

I want to collect the characters from 1-10 and 20-30 from each line of the file and take them in a file in the following format.Can someone help me with this : string1,string2 string1,string2 string1,string2 : : : : (7 Replies)
Discussion started by: cmsdelhi
7 Replies
Login or Register to Ask a Question