Awk- catching the last two chars


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk- catching the last two chars
# 1  
Old 11-21-2005
Awk- catching the last two chars

Can anyone explain to me how to get the last two chars' from each row of Column (each row being variable in length) using awk, some of the lines will be blank, I'll be running a paste after awking. So I need to keep the blanks where they are..so I can paste back all columns in the correct order

I see posts about stripping the last chars but nothing on getting the last chars

ie
GASTRO A9
NEONATALAF
GER.ASAB

GASTRO A9
ORSUR D3
GASTRO A9
GER.AS AB
GER.AP AB
NEONATALAF

output :

A9
AF
AB

A9
D3
A9
AB
AB
AF
# 2  
Old 11-21-2005
Question Suggestion

Maybe something like....
grep -C 2> filename
? im not sure im trying to learn grep, awk, and sed so im not sure. if anyone can explain what i did wrong please do. How i came up with my solution is grep for searching -C because in man grep it says -C means print num lines of output context and 2> for 2 before the end of the line. I am most likely wrong but if someone can show the right way and explain why im wrong it would be greatly appreciated. Thanks
# 3  
Old 11-21-2005
Another suggestion if the otherone doesnt work

sed -n '/STRING/,$p' filename|head -$n
Thats what i found while floating around on the forums now maybe you could change head to foot and possible make it so instead of the line and 2 more lines print, change it to make the last 2 character print? Idk maybe this will help you realize what you need or something? But can someone let me know whats wrong with this if it doesnt work maybe?
# 4  
Old 11-21-2005
I can't think of anything in awk I'm afraid. I would have thought it'd be custom made for that job (in fact I'm sure someone who knows more will give you a very simple command, but I've drawn a blank), but with sed you could use:

sed 's/^.*\(..\)$/\1/' input_file
# 5  
Old 11-21-2005
awk filtering the last two characters at end of line

thanks for that I'll give it a try in the morning when I get to work...
# 6  
Old 11-21-2005
Code:
nawk '{print substr($0, length($0)-1)}' myFile.txt

# 7  
Old 11-21-2005
You may Laugh!

since I dont have much experience in any thing: I will do this work in a simple but long way: so laughing is allowed looking at my code.

for file in `cat t40.txt|nawk '{print $3}'`;do
n=`echo $file|wc -C`
n=`expr $n - 1`
n1=`expr $n - 1`
echo $file|cut -c $n1-$n

I dont know why wc -C brings one more number than the real number of characters are there.

echo "this"|wc -C

this gives value 5 instead of 4.

thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. Shell Programming and Scripting

AWK splitting a string of equal parts of 500 chars

Hi , can someone help me how to make an AWK code for splitting a string of equal parts of 500 chars in while loop? Thank you! (4 Replies)
Discussion started by: sanantonio7777
4 Replies

3. Shell Programming and Scripting

AWK/SED: handle max chars in a line

Hi all, I hope you guys can help me. I prefer SED/AWK solutions if possible. For my shame it didn't work for me :o ISSUE: :wall: 1\3 1/$4\@7\ 1234567890123456789\ 1234567890123456789,\ 1234567890123456789\ 123456789012 12345 1234567890123456789\ 1234567890123456789,\ 1234... (5 Replies)
Discussion started by: unknown7
5 Replies

4. Shell Programming and Scripting

awk to print count for chars recurrence

input: 123456 1111 124567 2222 125678 3333 234567 aaaa 456789 abcd awk logic: - read lines for recurring 1st 2 chars of the 1st field - if recurrence detected count up and print value output: 1 123456 1111 2 124567 2222 3 125678 3333 (6 Replies)
Discussion started by: ux4me
6 Replies

5. Shell Programming and Scripting

Catching errors

Hi, I'm writing a scheduling script which will co-ordinate the launching of scripts. This script is scheduling based on an input file, and launches the appropriate scripts at the right times. The only issue I'm having is: - if a script dies, or even has a syntax error, I want to catch... (1 Reply)
Discussion started by: GoldenEye4ever
1 Replies

6. Programming

Signal catching

Hi! I want to catch all signals that my program receives print their name and then execute the default handler. Can you help me on that? I've tried the following code: #include <stdio.h> #include <unistd.h> #include <signal.h> void (*hnd)(int i); char signals = { "SIGHUP",... (7 Replies)
Discussion started by: dark_knight
7 Replies

7. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

8. Shell Programming and Scripting

Removing " " chars using Awk

HI Friends, I am trying to elliminate the " " characters from the word: "hello" using awk. I need the output to be just = hello (without " " chars). Is there any way to do this ? Thanks! (3 Replies)
Discussion started by: vijaya2006
3 Replies

9. Shell Programming and Scripting

How to convert C source from 8bit chars to 16bit chars?

I was using the following bash command inside the emacs compile command to search C++ source code: grep -inr --include='*.h' --include='*.cpp' '"' * | sed "/include/d" | sed "/_T/d" | sed '/^ *\/\//d' | sed '/extern/d' Emacs will then position me in the correct file and at the correct line... (0 Replies)
Discussion started by: siegfried
0 Replies
Login or Register to Ask a Question