views:

1144

answers:

8

Hi all -

I'm new to sed and awk - so I'm not really sure which is the most efficient way to go about this.

I'm looking to extract the first two letters of a string. I could do it if they were going to be same every time, but I can't seem to figure out how to just say,

Take n positions of this string from this larger string x.

IE. USCAGoleta9311734.5021-120.1287855805 = US

+3  A: 

easiest way is ${string:position:length} . Where this extracts $length substring from $string at $position.

This is a bash builtin so awk or sed is not required.

ennuikiller
+4  A: 

One method is to use cut:

echo 'USCAGol...' | cut -c1-2

gives you "US".

Another using bash with no external programs:

long=USCAGol...
short=${long:0:2}
echo ${short}
paxdiablo
A: 

Is this what your after?

my $string = 'USCAGoleta9311734.5021-120.1287855805';

my $first_two_chars = substr $string, 0, 2;

ref: substr

/I3az/

draegtun
given that he/she is likely to be calling this from the shell, a better form would be `perl -e 'print substr $ARGV[0], 0, 2' 'USCAGoleta9311734.5021-120.1287855805'`
Chas. Owens
A: 

if mystring = USCAGoleta9311734.5021-120.1287855805

print substr(mystring,0,2)

would print US

where 0 is the start position and 2 is how meny chars to read

Jambobond
Say...isn't that GW-BASIC? Oh, wait, that's `awk`. Sorry, I couldn't tell at first.
Dennis Williamson
A: 
perl -ple 's/^(..).*/$1/'
dsm
You forgot to echo the string into that.
Chas. Owens
No I didn't... it reads STDIN
dsm
+1  A: 

You've gotten several good answers and I'd go with the Bash builtin myself, but since you asked about sed and awk and (almost) no one else offered solutions based on them, I offer you these:

echo "USCAGoleta9311734.5021-120.1287855805" | awk '{print substr($0,0,2)}'

and

echo "USCAGoleta9311734.5021-120.1287855805" | sed 's/\(^..\).*/\1/'

The awk one ought to be fairly obvious, but here's an explanation of the sed one:

  • substitute "s/"
  • the group "()" of two of any characters ".." starting at the beginning of the line "^" and followed by any character "." repeated zero or more times "*" (the backslashes are needed to escape some of the special characters)
  • by "/" the contents of the first (and only, in this case) group (here the backslash is a special escape referring to a matching sub-expression)
  • done "/"
Dennis Williamson
A: 

colrm — remove columns from a file

To leave first two chars, just remove columns starting from 3

cat file | colrm 3
Ian Yang
A: 

If you're in bash, you can say:

bash-3.2$ var=abcd
bash-3.2$ echo ${var:0:2}
ab

This may be just what you need…

Dominic Mitchell
Sorry — I didn't see that Pax had already posted this.
Dominic Mitchell