Unix command to select first few characters and last character of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix command to select first few characters and last character of a line
# 1  
Old 05-17-2012
Unix command to select first few characters and last character of a line

I have a huge file and I want to select first 10 charcters and last 2 characters of everyline and than will filter the unique line.

I know, it must be easy bt I am new to unix scriptingSmilie

Ex.
I have file as below and need to e3kbaird and last 2 characters. and than unique records.

Code:
e3kbaird 20120402104958362 1
e3kbaird 20120402104958362 1
e3kbaird 20120402104958362 1
e3kbaird 20120402104958362 1
e3kbaird 20120402104958362 1
e3kbaird 20120402104958362 1
e3kbaird 20120402104958362 1


Last edited by Scrutinizer; 05-17-2012 at 04:39 AM.. Reason: code tags
# 2  
Old 05-17-2012
Code:
 
awk '{print $1,$NF}' test.txt | sort -u

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 05-17-2012
If you're using modern bash or ksh, try this:

To strip first 10 chars:
Code:
$ x='e3kbaird 20120402104958362 1'
$ echo "${x:0:10}"
e3kbaird 2
$

To strip the last 2 characters:
Code:
$ echo "${x: -2}"
 1
$

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 05-17-2012
Thanks ! It worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

2. Shell Programming and Scripting

How to use a multiple line list with the select command in ksh?

I copied the below program to play around with displaying a list of items using the select command in ksh. When I put all items in the same line, it works fine. I am trying to use multiple lines instead of a single row...my list is too large for a single line. How do I get the line continuation... (3 Replies)
Discussion started by: haganator
3 Replies

3. Shell Programming and Scripting

Delete characters from each line until meet character ":"

Hello, I have file that looks like this : 765327564:line1 94:line2 7865:line3 ..... 765322:linen I want to cut all the digits from the beginning of each line up to ":" character and to have everything like this : line1 line2 line3 ..... linen P.S : content of line1 ...... (8 Replies)
Discussion started by: black_fender
8 Replies

4. Shell Programming and Scripting

Unix-problem of New line character

Hi All, "Please read the below information carefully." i have tried the below code for counting the number of lines present in text file ignoring blank lines #! /bin/bash clear rdCount=0; while read myline do if ; then echo "line is empty" else echo $myline let... (10 Replies)
Discussion started by: aish11
10 Replies

5. UNIX for Dummies Questions & Answers

To bring all characters in one line in unix

How to bring all characters in one line in unix/solaris ? Eg : If it is a b c then it should show abc (2 Replies)
Discussion started by: mahen1naik
2 Replies

6. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

9. UNIX for Dummies Questions & Answers

How to select a particular media from the printer with a UNIX command

Problem Overview: We have a scheduler that prints report on any of the network printer. Problem Statement: We need to find a UNIX command that picks up either A4, legal or letter size paper form the printer. I found out a command but it's not working on our environment. ... (3 Replies)
Discussion started by: HelpMeOUt
3 Replies
Login or Register to Ask a Question