using awk to omit certain characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to omit certain characters
# 1  
Old 08-26-2010
using awk to omit certain characters

I have a file called file.txt with multiple lines in them. I'm trying to remove the first 2 characters and the last 2 with awk.

Code:
'.batman')
'.superman')
'.dragonheart')
.'superduper')

I tried doing the following and able to get rid of the first two characters. How do I remove the trailing characters?
Code:
cat file.txt | awk ' {print substr($0,3)}'

# 2  
Old 08-26-2010
Code:
awk '{print substr($0, 3, length-4)}'

Code:
sed 's/^..//; s/..$//'

# 3  
Old 08-26-2010
Thanks! that worked great. Do you mind explaining how does the length option work?
# 4  
Old 08-26-2010
substr takes input arguments as start point and number of char to print.
Length option will give u the length of the whole string. Now in the awk statement the number of char is given by length-4 , i.e. for the string '.batman') total length is 10 so awk will print from 3rd char till 9. Correct me if i am wrong. (alister )
# 5  
Old 08-26-2010
Code:
sed 's/^..\(.*\)..$/\1/' urfile

# 6  
Old 08-26-2010
I think this would suffice in this case given sed's greediness Smilie
Code:
sed 's/..\(.*\)../\1/' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine/omit data from 2 files

i made a script on my own. this is for the inventory to all of my AWS servers, and i run it to all of my servers to get the hostname, please look at file2. Then i need some data in file3 as well,. i need to combine them #cat file1 192.10.1.41 server.age.com ###### 192.10.0.40 ssh cant... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

awk command to omit trailer record in a file

I am trying to omit the trailer record in a variable width file I tried using awk 'NR >1 { print prev } { prev = $0 }' filename The above command is giving output but somehow it is trimming columns from the record. For example if my record has columns A,B,C,D The awk gives output as A,B,C ... (4 Replies)
Discussion started by: abhilashnair
4 Replies

3. UNIX for Dummies Questions & Answers

Last line omit

I have a csv file "file.csv" which has 5 records but when I check record count for file.csv in unix it shows as 6, the last line is empty and I need to omit and need for processing. Kindly help. Thanks (6 Replies)
Discussion started by: Prashanth B
6 Replies

4. Shell Programming and Scripting

how to omit the output of the at now command

Hi Everyone, I'm invoking the following command by a script: at now < /home/d01plmi1/rep/start_replication.sh > /dev/null/ 2>&1 but everytime is runs it put a message into the mail box of the userid executing the command. How can I redirect this so I can stop these notifications... (4 Replies)
Discussion started by: arizah
4 Replies

5. Shell Programming and Scripting

How to omit a part of the string

I have a file, f1 with content: File f1: abc_English_Dlr def_123_English_Dlr qwe_98_yu_English_Dlr dsw_1_English_Dlr I want to remove "_English_Dlr" from it. I used cut command but the problem is there may be a case for getting 1 or more fields before _English_Dlr . So Cut... (8 Replies)
Discussion started by: karumudi7
8 Replies

6. UNIX for Dummies Questions & Answers

omit strings from grep result

Hi, I know this is a really dumb question, and I used to know how to do this, but I forgot and I can't seem to find the command online anywhere: When I grep a string, how do I filter the results so that lines containing a certain string are OMITTED! for example: grep 'string' *.txt ... (2 Replies)
Discussion started by: juliette salexa
2 Replies

7. Shell Programming and Scripting

Omit Blank Lines while comparing two files.

Hello All, I am writting file comparison Utility and I have encountered such a senario where there are 2 files such as follows- 1#!/usr/local/bin/python 2 import gsd.scripts.admin.control.gsdPageEscalate 3.gsd.scripts.admin.control.gsdPageEscalate.main() 1 #!/usr/local/bin/python... (10 Replies)
Discussion started by: Veenak15
10 Replies

8. Shell Programming and Scripting

how to omit data from a file created in a script

I am using the fallowing script. this script seems to work fine except the file has data I do not wish to have. Is there away to omit that data. I will first provide the scrip and then a sample of the data the way it looks and then a sample of how I would like the data to look. Thanks for any... (3 Replies)
Discussion started by: krisarmstrong
3 Replies

9. Shell Programming and Scripting

AWK script to omit top characters with dashes

Hi I have an AWK script that takes an input file and outputs it as CSV format. The problem is its also outputting the characters at the top which are dashes(-------) and i want it to leave them out. My script is as follows. BEGIN { count=1; } /^/ { count+=1 if ( count > 2 ){... (3 Replies)
Discussion started by: magikminox
3 Replies

10. UNIX for Dummies Questions & Answers

Omit repeating lines

Can someone help me with the following 2 objectives? 1) The following command is just an example. It gets a list of all print jobs. From there I am trying to extract the printer name. It works with the following command: lpstat -W "completed" -o | awk -F- '{ print $1}' Problem is, I want... (6 Replies)
Discussion started by: TheCrunge
6 Replies
Login or Register to Ask a Question