Grep a particular string from column eliminating characters at the end.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a particular string from column eliminating characters at the end.
# 1  
Old 05-21-2018
Hammer & Screwdriver Grep a particular string from column eliminating characters at the end.

Hi,

So basically I have this file containing query output in seperated columns.

In particular column I have the below strings:
news
news-prio

I am trying to grep the string news without listing news-prio aswell.

I tried
Code:
grep "$MSG_TYPE"

,
Code:
grep -w "$MSG_TYPE"

,
Code:
grep "\<$MSG_TYPE\>"

, however none of them worked.

The variable MSG_TYPE is taken as an argument in the script.

Can someone provide an example on how I can achieve this?
# 2  
Old 05-21-2018
What column the string is in?
What separates columns?
A sample file would help as well...
# 3  
Old 05-21-2018
Quote:
Originally Posted by vgersh99
What column the string is in?
What separates columns?
A sample file would help as well...
A pipe seperates the column
Code:
q_1_2|Orange|news|0|0|0|0|43200|43200

The below is an example:

Code:
msg_type=`cat $CRONOUTFILE| grep $AFFILIATE | grep "$MSG_TYPE" | awk -F'|' '{print$3}' `


Last edited by rbatte1; 05-21-2018 at 12:17 PM.. Reason: Added CODE tags for sample data
# 4  
Old 05-21-2018
Code:
MSG='news'
awk -F'|' '$3 == msg' msg="${MSG}" "${CRONOUTFILE}"

# 5  
Old 05-21-2018
How about
Code:
grep "|$MSG_TYPE|" file
q_1_2|Orange|news|0|0|0|0|43200|43200

Please be aware that he pipe symbol has a special meaning in extended regexes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

3. UNIX for Dummies Questions & Answers

Removing characters from end of string

Hello, I have records like below that I want to remove any five characters from the end of the string before the double quotes unless it is only an asterik. 3919,5020 ,04/17/2012,0000000000006601.43,,0000000000000000.00,, 132, 251219,"*" 1668,0125 ... (2 Replies)
Discussion started by: jyoung
2 Replies

4. Shell Programming and Scripting

Remove lines that match string at end of column

I have this: 301205 0000030000041.49000000.00 2011111815505 908 301205 0000020000029.10000000.00 2011111815505 962 301205 0000010000027.56000000.00 2011111815505 3083 312291 ... (2 Replies)
Discussion started by: herot
2 Replies

5. Shell Programming and Scripting

Eliminating space constraint in grep

here in the below code just a space between 'Info' and '(' is showing that the patter doesnt match... echo "CREATE TABLE Info (" | grep -i "CREATE TABLE Info (" | wc | awk -F' ' '{print $1}' 1 echo "CREATE TABLE Info (" | grep -i "CREATE TABLE Info (" | wc | awk -F' ' '{print $1}' 0 ... (9 Replies)
Discussion started by: vivek d r
9 Replies

6. Shell Programming and Scripting

Append char to the end of string from Nth column

I'm sure this is easy to do but I can't find a one line command with awk or sed to append a char to the end of the string from Nth column. Any sugestion please? Thanks (2 Replies)
Discussion started by: cabrao
2 Replies

7. HP-UX

Eliminating characters between two expressions

Hi I have to eliminate all characters between ^ and a space in a file. Following lines - Test ^ H^@^@^@^@^@^@^B^VDM-BM-$|M-^_M-F^AM- File1 Test^H^@^@^@^@^@^F^A^X^@^SM-s^TM-3M-G^A File2 Should be printed as below Test File1 Test File2 I used sed '/^/,/ /d' command, but it is not working.... (1 Reply)
Discussion started by: arsheshadri
1 Replies

8. Shell Programming and Scripting

UrgentPlease: compare 1 value with file values eliminating special characters

Hi All, I have file i have values like ---- 112 113 109 112 109 I have another file cat supplierDetails.txt ------------------------- 112|MIMUS|krishnaveni@google.com 113|MIMIRE|krishnaveni@google.com 114|MIMCHN|krishnaveni@google.com 115|CEL|krishnaveni@google.com... (10 Replies)
Discussion started by: kittusri9
10 Replies

9. AIX

CUT command - cutting characters from end of string

Hello, I need to delete the final few characters from a parameter leaving just the first few. However, the characters which need to remain will not always be a string of the same length. For instance, the parameter will be passed as BN_HSBC_NTRS/hub_mth_ifce.sf. I only need the bit before the... (2 Replies)
Discussion started by: JWilliams
2 Replies

10. Shell Programming and Scripting

Removing characters from end of $string

I am writing a script to search PCL output and append more PCL data to the end accordingly. I need to remove the last 88 bytes from the string. I have searched for a few hours now and am coming up with nothing. I can't use head or tail because the PCL output is all on one line. awk crashes on... (3 Replies)
Discussion started by: craig2k
3 Replies
Login or Register to Ask a Question