Visit Our UNIX and Linux User Community


How to remove certain character strings with awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove certain character strings with awk?
# 1  
Old 04-03-2015
How to remove certain character strings with awk?

Hi all,

I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine).

Input:
Code:
 DBPATH=/some/path/database/db

Desired output:
Code:
/some/path/database

Thank you!

Last edited by rbatte1; 06-27-2016 at 05:47 AM.. Reason: Code tags
# 2  
Old 04-03-2015
Hello Ejianu,

Following may help you in same.
Code:
echo "DBPATH=/some/path/database/db" | awk '{sub(/.*=/,X,$0);print}'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-03-2015
RavinderSingh13,

Thank you for your prompt response. Unfortunately I get the following:

Code:
>echo "DBPATH=/some/path/database/db" | awk '{sub(/.*=/,X,$0);print}'
awk: syntax error near line 1
awk: illegal statement near line 1

# 4  
Old 04-03-2015
Hello ejianu,

On a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk, or nawk.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 04-03-2015
RavinderSingh13,

Thank you! You are correct, I am using a Solaris OS.

Using nawk and /usr/xpg4/bin/awk I get the following, which is closer to what I need, but I still get /db at the end. How can I remove the /db also?

Code:
>echo "DBPATH=/some/path/database/db" | nawk '{sub(/.*=/,X,$0);print}'
/some/path/database/db

>echo "DBPATH=/some/path/database/db" | /usr/xpg4/bin/awk '{sub(/.*=/,X,$0);print}'
/some/path/database/db

>echo "DBPATH=/some/path/database/db" | /usr/xpg6/bin/awk '{sub(/.*=/,X,$0);print}'
ksh: /usr/xpg6/bin/awk:  not found

# 6  
Old 04-03-2015
Hello ejianu,

Please try following(sorry I didn't see you don't need db also).
Code:
echo "DBPATH=/some/path/database/db" | nawk '{sub(/.*=/,X,$0);match($0,/\/.*\//);print substr($0,RSTART,RLENGTH-1)}'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 04-03-2015
It works. Thank you!

Previous Thread | Next Thread
Test Your Knowledge in Computers #246
Difficulty: Easy
By 1973, e-mail constituted 95% of the ARPANET traffic.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove character \r and \n in awk

Hi Everybody: I need your help, please... I have this file *.txt 0000 | 16010201 22000000 67892000 00000000 00000000 00000100 72246681 28E08236 | ~~~~"~~~g~ ~~~~~~~~~~~~~r$f~(~~6 | 0020 | 10476173 90010100 10000000 00000001 05000226 17163011 12442212 48140484 |... (2 Replies)
Discussion started by: solaris21
2 Replies

2. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

3. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. 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

5. Shell Programming and Scripting

Want to remove / and character using awk or sed

Below i am trying to remove "/" and "r" from the output, so i need output as: hdiskpower3 hdisk0 hdisk1 #inq | grep 5773 | awk '{print $1}' | sed 's/dev//g' | awk -F"/" '{$1=$1}1' .....................................................//rhdiskpower0 //rhdiskpower1 //rhdiskpower2... (3 Replies)
Discussion started by: aix_admin_007
3 Replies

6. Shell Programming and Scripting

awk or sed script to remove strings

Below am trying to separate FA-7A:1, In output file it should display 7A 1 Command am using Gives same output as below format: 22B7 10000000c9720873 0 22B7 10000000c95d5d8b 0 22BB 10000000c97843a2 0 22BB 10000000c975adbd 0 Not showing FA ports as required format... (5 Replies)
Discussion started by: aix_admin_007
5 Replies

7. Shell Programming and Scripting

awk based script to ignore all columns from a file which contains character strings

Hello All, I have a .CSV file where I expect all numeric data in all the columns other than column headers. But sometimes I get the files (result of statistics computation by other persons) like below( sample data) SNO,Data1,Data2,Data3 1,2,3,4 2,3,4,SOME STRING 3,4,Inf,5 4,5,4,4 I... (9 Replies)
Discussion started by: ks_reddy
9 Replies

8. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

9. UNIX for Dummies Questions & Answers

character-by-character comparison of strings

This might be a dummy question, but is there a command in UNIX that compare two strings character-by-character and display the difference? ---------- Post updated at 11:25 AM ---------- Previous update was at 10:32 AM ---------- Or probably what I'm looking is how to break a string into... (3 Replies)
Discussion started by: Orbix
3 Replies

10. Shell Programming and Scripting

Perl RegExp to remove last character from strings

I use SAS (a statistical software) and have to remove last character or the last 1/2 numbers that appear after characters from the string using Perl Regular Expression (which is recognized by SAS). Input: f183ii10 f183ii2 f182ii1 f182ii2 f183iim f22ii f22ii11 f22ii12 pmh4 pmhm Desired... (2 Replies)
Discussion started by: ospreyeagle
2 Replies

Featured Tech Videos