![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to convert C source from 8bit chars to 16bit chars? | siegfried | Shell Programming and Scripting | 0 | 09-26-2007 11:26 AM |
| catching some errors | bebop1111116 | Shell Programming and Scripting | 5 | 09-29-2006 10:12 AM |
| Catching all Exit Codes | Sivaswami J | Shell Programming and Scripting | 3 | 02-16-2006 04:13 PM |
| Catching signal and piping | joseph_ng | High Level Programming | 7 | 11-14-2005 08:04 AM |
| catching interrupts | toughguy2handle | UNIX for Dummies Questions & Answers | 1 | 09-19-2005 12:17 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
nawk '{print substr($0, length($0)-1)}' myFile.txt
|
|
#7
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |