Sponsored Content
Top Forums Shell Programming and Scripting Remove first 2 characters and last two characters of each line Post 302987908 by RavinderSingh13 on Sunday 18th of December 2016 12:16:16 AM
Old 12-18-2016
Hello SkySmart,

Could you please try following and let me know how it goes then, though I am not sure about your complete requirement.
Code:
cat script.ksh 
FIRST_TWO=$(awk '{Q=Q?Q "@@" substr($0,1,2):substr($0,1,2)} END{print Q}' file1)
LAST_TWO_CHAR=$(awk '{Q=Q?Q "@@" substr($0,length($0)-1,2):substr($0,length($0)-1,2)} END{print Q}' file1)
REST_CHAR=$(awk '{Q=Q?Q "@@" substr($0,3,length($0)-4):substr($0,3,length($0)-4)} END{print Q}' file1)

echo "Separate ones:"
echo "First 2 chars of each line: " $FIRST_TWO
echo "Last 2 chars of each line: " $LAST_TWO_CHAR
echo "Lines without first 2 chars and last 2 chars:" "$REST_CHAR"


echo "combining the lines again here..."
awk -vfirst_two="$FIRST_TWO" -vlast_two_char="$LAST_TWO_CHAR" -vrest_char="$REST_CHAR" 'BEGIN{num=split(first_two, A,"@@");split(last_two_char, B,"@@");split(rest_char, C,"@@");for(i=1;i<=num;i++){print A[i] C[i] B[i]}}'

So when we are running the script then following will be the output.
Code:
./script.ksh 
Separate ones:
First 2 chars of each line:  1h@@1l@@19@@1w@@22@@ZG@@66
Last 2 chars of each line:  RU@@5k@@5S@@Rq@@kw@@FV@@Ft
Lines without first 2 chars and last 2 chars: sRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTn@@KUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG@@5Um5kaFZ6VnFURzFPZG1KVFFsQlZhVUp0WTFkU2RVOXVRbTVqClIyZzBZek5TYWxsWVFYZE@@xWTBkb05FeHRaR2hqUjJ4MVdYazFhbUl5TUdkVU1VbG5DbHB1Um10aWFuQjNXak5DYjJWSV@@oKTWtaM1RVUkJla3h1UW05bFF6VnVXVmhDY0dKdFRYVlpNamwwU1VVNVUwbEhXbmhhUnpRMl@@tkMkZJYUhwa1IwNW9ZMFJCZDA1RApOWGNLWVVobmRWb3lSbmRoVnpWcVRHMU9kbUpUUWxCVm@@qRZMWRTZFU5dVFtNWpSMmcwWXpOU2FsbFlRWGROUkZWMVkwZG9ORXh0ClpHaGpSMngxV1hrMW
combining the lines again here...
1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU
1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k
195Um5kaFZ6VnFURzFPZG1KVFFsQlZhVUp0WTFkU2RVOXVRbTVqClIyZzBZek5TYWxsWVFYZE5S
1wxWTBkb05FeHRaR2hqUjJ4MVdYazFhbUl5TUdkVU1VbG5DbHB1Um10aWFuQjNXak5DYjJWSVRq
22oKTWtaM1RVUkJla3h1UW05bFF6VnVXVmhDY0dKdFRYVlpNamwwU1VVNVUwbEhXbmhhUnpRMlkw
ZGtkMkZJYUhwa1IwNW9ZMFJCZDA1RApOWGNLWVVobmRWb3lSbmRoVnpWcVRHMU9kbUpUUWxCVmFV
66qRZMWRTZFU5dVFtNWpSMmcwWXpOU2FsbFlRWGROUkZWMVkwZG9ORXh0ClpHaGpSMngxV1hrMWFt

This is just an example where you could use the first 2 characters, last 2 characters and rest of the line without them in a script itself rather than saving them into files. If you have more requirements then kindly do let us know on same.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

how to remove line greater then 3000 characters.

I am using awk and it stops when it encounter line greater then 3000 character. Is there any command which will help me remove line greater then 3000 characters. (10 Replies)
Discussion started by: naren_14
10 Replies

2. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies

3. UNIX for Dummies Questions & Answers

remove characters from line

Hello, I have multiple lines in a file, each of which will have data that looks like this: xxxxxyyyyzzzz4abcdXYZXYZXYZ pqrstPQRST2cdPQRSTPQRST lmnopqr6abcdefgRST.3abc I want to be able to remove the number 4 + the following 4 characters (abcd) in the first line. For the second line,... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

4. Shell Programming and Scripting

How to remove special characters from each line?

Hello, Is there a simpler way to remove special characters (color codes) from each lines in a log file? I use sed like in the example below but I think there should be a more simple way to achieve the same result: $ cat -vet file1 ^, , , , Maybe to convert the file somehow? ... (5 Replies)
Discussion started by: majormark
5 Replies

5. UNIX for Advanced & Expert Users

Remove new line characters if found between 1 to 10 columns

Hi, I have a file with ';' delimeter which has some new line characters. How can I delete the new line characters if they are found between 1 to 10 fields. Thanks (3 Replies)
Discussion started by: rudoraj
3 Replies

6. UNIX Desktop Questions & Answers

Remove new line characters from a file

I tried using below command tr -cd "" < InputFile.xml > output.txt ============= This removes all the tabs/newline/extra spaces from a file it successfully removed all the extra spaces,tabs and new line characters but then the complete file become one record. I want to retain one new line... (1 Reply)
Discussion started by: saini
1 Replies

7. Shell Programming and Scripting

remove first few characters from each line

Hi, I have a file with lines like below. I need to remove first few characters from each line until a date format is found. 05/06/12 20:47:02 GUMGUY@98.192.174.74{42B42A72AC955F5926621273E3A15059.tomcat2}TP-Processor15 LogExchUsage: ERROR: 05/06/12 20:47:02... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

8. Shell Programming and Scripting

Remove characters from line

I have a the following line 22.152.25.36 - K##### "GET /DGGKE/GetMail.do;jwebsphere=FADFFFGSFGSFGSDGFSDFGSDFGSDF HTTP/1.1" 200 44948 Need a cut command which should give me the below output 22.152.25.36 - K##### "GET /DGGKE/GetMail.do HTTP/1.1" 200 44948 Note: The value of jwebsphere can... (6 Replies)
Discussion started by: suindar1982
6 Replies

9. Shell Programming and Scripting

I want to remove 1st and last two characters of each line of the file

I want to remove 1st and last two characters of each line of the file Ex: file1 zzfile1ee @xfile2:y qfile3>> @ file4yy and redirect to the file called new Basically file will have any charcter including space, spical character... Please help.... (7 Replies)
Discussion started by: shell1509
7 Replies

10. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies
A2P(1)							 Perl Programmers Reference Guide						    A2P(1)

NAME
a2p - Awk to Perl translator SYNOPSIS
a2p [options] [filename] DESCRIPTION
A2p takes an awk script specified on the command line (or from standard input) and produces a comparable perl script on the standard out- put. OPTIONS Options include: -D<number> sets debugging flags. -F<character> tells a2p that this awk script is always invoked with this -F switch. -n<fieldlist> specifies the names of the input fields if input does not have to be split into an array. If you were translating an awk script that processes the password file, you might say: a2p -7 -nlogin.password.uid.gid.gcos.shell.home Any delimiter can be used to separate the field names. -<number> causes a2p to assume that input will always have that many fields. -o tells a2p to use old awk behavior. The only current differences are: * Old awk always has a line loop, even if there are no line actions, whereas new awk does not. * In old awk, sprintf is extremely greedy about its arguments. For example, given the statement print sprintf(some_args), extra_args; old awk considers extra_args to be arguments to "sprintf"; new awk considers them arguments to "print". "Considerations" A2p cannot do as good a job translating as a human would, but it usually does pretty well. There are some areas where you may want to examine the perl script produced and tweak it some. Here are some of them, in no particular order. There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always inte- ger anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always going to be integer, so it leaves it in. You may wish to remove it. Perl differentiates numeric comparison from string comparison. Awk has one operator for both that decides at run time which comparison to do. A2p does not try to do a complete job of awk emulation at this point. Instead it guesses which one you want. It's almost always right, but it can be spoofed. All such guesses are marked with the comment ""#???"". You should go through and check them. You might want to run at least once with the -w switch to perl, which will warn you if you use == where you should have used eq. Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced. If somehow you are relying on this mechanism to create null entries for a subsequent for...in, they won't be there in perl. If a2p makes a split line that assigns to a list of variables that looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n option mentioned above. This will let you name the fields throughout the script. If it splits to an array instead, the script is probably referring to the number of fields somewhere. The exit statement in awk doesn't necessarily exit; it goes to the END block if there is one. Awk scripts that do contortions within the END block to bypass the block under such circumstances can be simplified by removing the conditional in the END block and just exiting directly from the perl script. Perl has two kinds of array, numerically-indexed and associative. Perl associative arrays are called "hashes". Awk arrays are usually translated to hashes, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. Itera- tion over a hash is done using the keys() function, but iteration over an array is NOT. You might need to modify any loop that iterates over such an array. Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. You'll want to set $# explicitly if you use the default value of OFMT. Near the top of the line loop will be the split operation that is implicit in the awk script. There are times when you can move this down past some conditionals that test the entire record so that the split is not done as often. For aesthetic reasons you may wish to change the array base $[ from 1 back to perl's default of 0, but remember to change all array sub- scripts AND all substr() and index() operations to match. Cute comments that say "# Here is a workaround because awk is dumb" are passed through unmodified. Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself. Scripts that refer to the special variables RSTART and RLENGTH can often be simplified by referring to the variables $`, $& and $', as long as they are within the scope of the pattern match that sets them. The produced perl script may have subroutines defined to deal with awk's semantics regarding getline and print. Since a2p usually picks correctness over efficiency. it is almost always possible to rewrite such code to be more efficient by discarding the semantic sugar. For efficiency, you may wish to remove the keyword from any return statement that is the last statement executed in a subroutine. A2p catches the most common case, but doesn't analyze embedded blocks for subtler cases. ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n]. A loop that tries to iterate over ARGV[0] won't find it. ENVIRONMENT
A2p uses no environment variables. AUTHOR
Larry Wall <larry@wall.org> FILES
SEE ALSO
perl The perl compiler/interpreter s2p sed to perl translator DIAGNOSTICS
BUGS
It would be possible to emulate awk's behavior in selecting string versus numeric operations at run time by inspection of the operands, but it would be gross and inefficient. Besides, a2p almost always guesses right. Storage for the awk syntax tree is currently static, and can run out. perl v5.8.9 2005-03-10 A2P(1)
All times are GMT -4. The time now is 09:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy