solved - removing last n char in every line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers solved - removing last n char in every line
# 1  
Old 07-29-2010
solved - removing last n char in every line

Hi all,
I want to remove last n char in every line. I'm having the comment as,

[COLOR=black][FONT=Verdana]
Code:
cnt=10
cat filename | rev | cut -c $cnt- | rev


I want to have it with sed or awk or any command except perl. Pls help me in the comment. I have tried few options using sed. But not able to get it.

Thanks,
Poova.

Last edited by Yogesh Sawant; 08-04-2010 at 09:05 AM.. Reason: code tags, please!
# 2  
Old 07-29-2010
Code:
sed 's/.$//' filename

# 3  
Old 07-29-2010
Code:
string="abcde"
len=${#string}
cnt=2
echo ${string:0:$((len-cnt))}

# 4  
Old 07-30-2010
Thanks,
Do we have it in sed without identifying length and using it in echo?

---------- Post updated at 09:39 AM ---------- Previous update was at 09:36 AM ----------

Quote:
Originally Posted by vgersh99
Code:
sed 's/.$//' filename

If i want to pass no.char to be removed from last in a variable then How can we write the command?
# 5  
Old 07-30-2010
Like this ?

Code:
# cat infile
testt
test1
test11
testz
testX

Code:
 
# sed 's/[^0-9]$//' infile
test
test1
test11
test
test

# 6  
Old 08-03-2010
using Perl:
Code:
perl -nl -e 'print substr($_, 0, (length($_) - 3))' file.txt

edit: replace 3 with number of characters that you want to omit from the end of line
# 7  
Old 08-03-2010
You can pass a variable to sed as shown here:
Code:
# cat infile
testt
test1
test11
testz
testX

Code:
$ CNT=3
$ echo $CNT
3
$ sed "s/.\{${CNT}\}$//" infile
te
te
tes
te
te
$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Modifying/Removing Timestamps from a filename

So given filenames of varying lengths, I was wondering how I would remove or modify appended timestamps of the current date DD-MM-YY. So say: test_DD-MM-YY.txt coolbeans_DD-MM-YY.pdf And what I expect the output to be: test.txt coolbeans.pdf Thanks :) (2 Replies)
Discussion started by: sodaboyz
2 Replies

2. Shell Programming and Scripting

How to separate one line to mutiple line based on one char?

Hi Gurus, I need separate one file which is one huge line to mutiple line. file like abcd # bcd # def # fge # ged I want to get abcd bcd def fge ged Thanks in advance (4 Replies)
Discussion started by: ken6503
4 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Removing an 8 character string

Edit: Figured it out. Close the thread please. Solution: \{8}\] edit by bakunin: no need to close the thread, but i changed the title to SOLVED. Thanks for writing a follow-up. (0 Replies)
Discussion started by: unknownn
0 Replies

4. Programming

C++ Removing = from char*

I have a char* str. Suppose str="medium"; I want to check if str has "=" as first non blank position, if it exists, I want to remove it Currently I am checking using if (optarg == '=') cout << "Found =" << endl; (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

6. UNIX for Dummies Questions & Answers

[solved]removing characters from a mass of file names

I found a closed thread that helped quite a bit. I tried adding the URL, but I can't because I don't have enough points... ? Modifying the syntax to remove ! ~ find . -type f -name '*~\!]*' | while IFS= read -r; do mv -- "$REPLY" "${REPLY//~\!]}"; done These messages are... (2 Replies)
Discussion started by: rabidphilbrick
2 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Help with using tr - Removing white spaces

Hi, I have a file that contains whitespaces with spaces and spaces and tabs on each line and am wanting to remove the whitespaces. My version of sed is one that does not recognize \t etc. The sed and awk one-liners below that I found via Google both does not work. So my next best... (3 Replies)
Discussion started by: newbie_01
3 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

Removing special char's with sed

Hi, I've a txt file which contains the following kind of listed data 18971 ./aosrp18.r 15340 ./aosrp12.r 22996 ./aosrp08.r 17125 ./aosrp06.r I'm trying to get rid of the ./ in the file and have tried the following with sed but I'm not getting the correct result... I'm not sure what way... (7 Replies)
Discussion started by: Jazmania
7 Replies

10. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies
Login or Register to Ask a Question