Truncate the content within alt attribute to first 250 characters.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate the content within alt attribute to first 250 characters.
# 1  
Old 02-07-2009
Truncate the content within alt attribute to first 250 characters.

I have a xml file which contains image tag as follows:

<image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450" alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain" type="photograph" orient="portrait"/></image>

Now, i want to write a script that will count whether the number of characters within alt attribute are greater than 250 and if it is; the data within alt attribute should be truncated to contain only first 250 characters.

It would be really nice if anybody could provide me the way to do so.
# 2  
Old 02-07-2009
You know, if it truncated it in the middle of an &#x2022; or somesuch the result could be invalid HTML.
# 3  
Old 02-07-2009

This truncates the alt attribute by item, rather than characters:

Code:
altcheck() #@ USAGE: altcheck [max]
{ #@ Truncate the alt attribute in $line to $max characters
  max=${1:-250}
  right=${line#*alt=\"}
  left=${line%"$right"}
  alt=${right%%\"*}
  right=${line#*"$alt"}
  while [ ${#alt} -gt $max ]
  do
    alt=${alt% &*}
  done
  line=$left$alt$right
}

while IFS= read -r line
do
  case $line in
    *"<img"*alt=*) altcheck ;;
  esac
  printf "%s\n" "$line"
done < "$FILE"

# 4  
Old 02-10-2009
Hi Johnson!

Thanks for your help! . But above script is not working. Please help!!!. Thanks.
# 5  
Old 02-10-2009
Hammer & Screwdriver Another way to look at this

I duplicated some of the text inside that alt area so I could show it trimmed down. And then trimmed it at 150.
Note that it does not necessarily break nicely, and does not address the quotation " character.
However, this logic appears to trim on that field.

Code:
> cat file164
<image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450" alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain" type="photograph" orient="portrait"/></image>
<image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450" alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain" type="photograph" orient="portrait"/></image>

> sed "s/alt/~alt/g" file164 | sed "s/type/~type/g" | awk -F"~" '{print $1,substr($2,1,150),$3}'
<image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450"  alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain"  type="photograph" orient="portrait"/></image>
<image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450"  alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain Brazil &#x2022; Japan &#x2022; Kor type="photograph" orient="portrait"/></image>

# 6  
Old 02-11-2009
Quote:
Originally Posted by parshant_bvcoe
But above script is not working.

What does "not working" mean? What does happen?

Where does the script fail?

Are there any error messages?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to truncate a string to x number characters?

Hello: I have a large file which contains lines like the following: 1/t123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00% /t is a tab, spaces are as indicated the string "&Xx:1:1234:12345:123456$ABCDEFG..." has a slightly variable number of numbers and letters, but it always starts... (9 Replies)
Discussion started by: Tectona
9 Replies

2. AIX

alt clone

I have two identical systems p740 with 2 lpars in each server. I dont want to install os from scratch on each lpar neither I want to use nim nor mksysb. Will it work perfectly if I install a fresh OS on 1 lpar and do alt disk clone for other lpras. My each lpar is having 2 physical disks. Now if... (2 Replies)
Discussion started by: vjm
2 Replies

3. Shell Programming and Scripting

Matching a pattern 250 characters up and down stream

hii all i have a file a which contains some thing like this strand smthg position + yyx 3020 - yyw 10,000 now i have another file (file2) which contains the data starting from 1 to n positions i want to refer first file if + ... (4 Replies)
Discussion started by: anurupa777
4 Replies

4. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

how to delete special characters from the file content

Hello Team, Any one suggest how to delte the below special character from a file which is having one column 10 rows of same below content. ---------------------------------------- Kosten|bersicht gemd_ ' =Welche Kosten kvnnen... (2 Replies)
Discussion started by: kanakaraju
2 Replies

6. Shell Programming and Scripting

Truncate file name to 40 characters

Hello all. I would like to make a script (or two shell scripts) that will do the following. I need the maximum file name and directory name to be 38 characters long. As well, if shortening the file name ends up making all of the files in that directory have the same name, then I would like... (9 Replies)
Discussion started by: marcozd
9 Replies

7. UNIX for Dummies Questions & Answers

Alt - Characters

in Win, one can use the Alt + 3-digit numeric code combination to spawn any ASCII character. Linux, OTOH has Ctrl-Shift-U + alphanumeric code combination to spawn any UNICODE character. bottom line: I'd like to be able to do what Win can do, please. I was told it was possible, but made... (5 Replies)
Discussion started by: ialoq
5 Replies

8. UNIX for Dummies Questions & Answers

Truncate last <n> characters from a file

I am trying to concatenate 2 files, but before concatenation, I would like to strip off the final character from the first file. The final character is a form feed (ascii 012 / hex 0C) and there will be an unknown number of these characters in the file. It is only the very last one which I want... (1 Reply)
Discussion started by: Gwailo88
1 Replies

9. UNIX for Dummies Questions & Answers

alt boot

ok guys, I have been given a big project, I need to implement alt boot on over 40+ servers, mainly solaris 8 and AIX 4.3, AIX 5.1. A couple of questions 1)can someone describe alt boot, my take is that if the primary disk fails(disk with root filesystems), this altboot disk takes over 2)can... (4 Replies)
Discussion started by: csaunders
4 Replies
Login or Register to Ask a Question